1

I have a COLLECTION collflokks in MongoDB, sample Document is :-

{   
    "_id" : "b_8AUL",   
    "f_name" : "Pizza. Hut",   
    "f_lat" : "22.7523513",   
    "f_lng" : "75.9225847",   
    "c_uid" : "33",   
    "f_type" : NumberLong(3),   
    "members" : [     
        "42",     
        "43"   
    ]   
}   

Within the "members" array , I want to add Arrays like {id:42,name:Mark} , {id:43,name:Hughes} Currently i'm adding just ids(eg.42,43). I'm only concerned about the new data as it will have new ids .Please suggest.

Earlier I was using this code to push into the members Array:

$flokkCollection = 'collFlokks';
$flokkCollection->update(
       array("_id" => $f_handle), 
       array('$push' => array("members" => $u_id))
);
0

1 Answer 1

1

Well if what you are asking here is "replacing your existing data" then you need to "loop" the results from the collection and "replace" the array content that exists with your new format.

There are likely smarter ways to approach this, but you are not really giving us all the required information in your question, so I can only answer in the basic terms.

Presuming you have:

$required = array(
   array(array("id" => "42"), array("name" => "Mark")),
   array(array("id" => "43"), array("name" => "Hughes"))
);

As input, then you do something like this:

function myMapper($v) {
    return $v["id"];
}

$mapped = array_map("myMapper",$required);

foreach( $mapped as $value) {

    $filtered = array_values(
        array_filter($required,function($k) {
            return $k["id"] == $value;
        })
    )[0];

    collection.update(array(
        array("members" => $value),
        array('$set' => array( 
            "members.$" => $filtered
        ))
    ));
}

Which should use the positional $ operator to find the matched "position" of the array element by the value used in the "query" portion of the update statement, then in the "update" portion of that statement $set that current array index to the new value at the "filtered" content index from the original input array.

Outside of PHP. We call these inner elements "objects" and not "arrays" which is a PHP notation trait. Key/value things are "objects" and "lists" are "arrays".

Sign up to request clarification or add additional context in comments.

9 Comments

I'm Sorry to have not provided proper info as I'm not used to asking Questions here. Actually , I wanted to change the functionality to push into the array with the new way - id & name ... i think I solved it using :- $user_data = array( 'uid' => $u_id, 'name' => "Dishu" ); array('$push' => array("members" => $user_data)));
@dgcoder It's not that much of an issue beyonf my point is that there are probably better ways to get the mapping of "Mark" and "Hughes" to the relevant "id" values in what you are really trying to do. This should at least give a guide to the general process anyway.
@dgcoder You didn't "solve it". You either need to "remove" or "replace" as this example shows you how to do, the existing array members. Otherwise you just end up with the existing array members and the addition of "new" members, which is clearly what you do not want.
Now my members section of document looks like : "members" : [ { "uid" : "42", "name" : "Dishu" }
Thanks @Blakes, Actually existing data does not matter to me as of now because i'm still developing my product. However, this code will surely help me if I need to change an existing Data.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.