i have collection collflokks in mongodb, sample document :-
{ "_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 "members" array , want add arrays {id:42,name:mark} , {id:43,name:hughes} i'm adding ids(eg.42,43). i'm concerned new data have new ids .please suggest.
earlier using code push members array:
$flokkcollection = 'collflokks'; $flokkcollection->update( array("_id" => $f_handle), array('$push' => array("members" => $u_id)) );
well if asking here "replacing existing data" need "loop" results collection , "replace" array content exists new format.
there smarter ways approach this, not giving required information in question, can answer in basic terms.
presuming have:
$required = array( array(array("id" => "42"), array("name" => "mark")), array(array("id" => "43"), array("name" => "hughes")) ); as input, this:
function mymapper($v) { return $v["id"]; } $mapped = array_map("mymapper",$required); foreach( $mapped $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 positional $ operator find matched "position" of array element value used in "query" portion of update statement, in "update" portion of statement $set current array index new value @ "filtered" content index original input array.
outside of php. call these inner elements "objects" , not "arrays" php notation trait. key/value things "objects" , "lists" "arrays".
Comments
Post a Comment