简体   繁体   中英

Add new element in nested array in mongodb php

I want to add a new element in array appusergoogleid. But I am getting error that i am adding object in array.

$id=array("id"=>$appusergoogleid);

$collection->update(array("appuseremail"=>$appuseremail),array('$push' => array('appusergoogleid'=>$id)));

This is the json in which im inserting

 { 
   "_id":ObjectId("55cc982946449835298b4567"),
   "appuserfirstname":"demo",
   "appuserlastname":" appuserlastname",
   "appusermiddlename":"xyz",
   "appuseremail":"abc@gmail.com",
   "follows":"1",
   "appusergoogleid":{ 
      "id":"wemhjgh"
   },
   "usercreationdate":ISODate(   "2015-08-13T18:44:17Z"   ),
   "status":"0"
}

i want that whenever i add something in appusergoogleid, it makes an array there instead of object.

$filter = array('_id'=>$id));
$update = array('$addToSet'=>array('done_by'=>'2'));
$q->update($filter,$update);

When you need to update use $addToSet so you avoid duplicate inserts which leads to multiple entries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM