Friday 20 February 2015

mongoDB : $pullAll : Remove specified values from existing array


The $pullAll operator removes all instances of the specified values from an existing array.

Syntax
{ $pullAll: { <field1>: [ <value1>, <value2> ... ], ... } }


> db.sample.find()
{ "_id" : 1, "arr" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] }
{ "_id" : 2, "arr" : [ 9, 8, 7, 6, 5, 4, 3, 2, 1 ] }
{ "_id" : 3, "arr" : [ 19, 6, 14 ] }
{ "_id" : 4, "arr" : [ 109, 208, 3, 6, 5, 104, 302, 280, 161 ] }
{ "_id" : 5, "arr" : [ 1, 6, 5, 5, 4 ] }


1.Remove elements 280, 1, 208, 109 from Array arr, for the document 4.
> db.sample.update({"_id" : 4}, {$pullAll : {"arr" : [280, 1, 208, 109]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.sample.find({"_id" : 4})
{ "_id" : 4, "arr" : [ 3, 6, 5, 104, 302, 161 ] }

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment