MongoDB Positional Operator $ -
i got following document
{ _id : 91, "myarray" : [ { "a" : "1", "b" : true }, { "a" : "1", "b" : true }, { "a" : "1", "b" : true } ] }
and want update each myarray.b
element false
b
true
. running following query:
db.mydb.update({"arr.b" : true},{$set : {"arr.$.b":false}},false, true)
and getting first array element updated not subdocuments.
{ _id : "91", "myarray" : [ { "a" : "1", "b" : false }, { "a" : "1", "b" : true }, { "a" : "1", "b" : true } ] }
all sub-documents match arr.b:true criteria should updated arr.b = false
. according video mongodb university situation possible.
i don't want use javascript function.
what missing? thanks
unfortunately updating documents in array not possible - see mongodb jira ticket use positional operator update items in array.
you can update each document manually:
db.mydb.find({}).foreach(function(doc) { doc.myarray.foreach(function(item) { if (item.b === true) item.b = false; }); db.mydb.update({ "_id": doc._id }, { "$set": { "myarray": doc.myarray }}); });
Comments
Post a Comment