How to update multiple MongoDB subdocuments in one shot? -
i have created couple of documents in mongodb:
> db.mycollection.insert( {"bucket": [{"field1":"x"},{"field2":{"a":"b","c":"d","e":"f"}}]} ) > db.mycollection.insert( {"bucket": [{"field1":"y"},{"field2":{"g":"h","i":"j","k":"l"}}]} ) > db.mycollection.find() { "_id" : objectid("534102c492970f1b06b1edc8"), "bucket" : [ { "field1" : "x" }, { "field2" : { "a" : "b", "c" : "d", "e" : "f" } } ] } { "_id" : objectid("5341054e92970f1b06b1edc9"), "bucket" : [ { "field1" : "t" }, { "field2" : { "g" : "h", "i" : "j", "k" : "l" } } ] }
now wold update such values of "bucket" set []
so try this:
db.mycollection.update({},{"bucket":[]}, false, false)
this partially worked. changed first bucket []:
> db.mycollection.find() { "_id" : objectid("534102c492970f1b06b1edc8"), "bucket" : [ ] } { "_id" : objectid("5341054e92970f1b06b1edc9"), "bucket" : [ { "field1" : "t" }, { "field2" : { "g" : "h", "i" : "j", "k" : "l" } } ] }
so tried change "multi" field true , tried again. didn't work:
> db.mycollection.update({},{"bucket":[]}, false, true) multi update works $ operators
how can update multiple subdocuments using update() method?
what need $set
operator update query work. try this:
db.mycollection.update({},{$set:{"bucket":[]}}, false, true)
Comments
Post a Comment