简体   繁体   中英

How can we remove an item in sub array in mongodb?

I have a user document structured as:

{
"_id" : ObjectId("5173b10fd0aa9af00d06395c"),
"customer_id" : 1,
"customer_fullname" : "Douglas E. Kelly",
"customer_email" : "1@1.com",
"customer_cityid" : "1",
"customer_selections" : [15, 952, 17347, 343102, 20380, 51757, 235961, 119824, 134011,   168251, 297596, 175792, 196010, 210349, 226945, 250102, 274064, 328834, 354893, 472868,  555878]
}

So I want to delete a number / an item in "customer selections". How can we do this?

UPDATE: I am using this query. But it does not remove the number

  $response=$collection->update(array('customer_id' =>1), array('$pull' => array('customer_selections'=>$selectedcompanyid)));

var_dump($response);

 array(5) { ["updatedExisting"]=> bool(true) ["n"]=> int(1) ["connectionId"]=> int(3) ["err"]=> NULL ["ok"]=> float(1) }

By using $pull

http://docs.mongodb.org/manual/reference/operator/pull/

db.yourColl.update( { id: ObjectId("5173b10fd0aa9af00d06395c") }, { $pull: { customer_selections: 952 } } )

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