javascript - Update key value where different key equals some value -
i have many documents (by unique toolname
) following structure, in db test
:
{ "_id" : objectid("111111111111"), "toolname" : "hammer", "abilities" : [ { "foo" : "blue", "bar" : "beagle", "loe" : 0.65 }, { "foo" : "red", "bar" : "beagle", "loe" : 0.57 }, { "foo" : "red", "bar" : "fish", "loe" : 0.42 } ] }
i can find
document following query:
db.test.find({"abilities.bar":"beagle","abilities.foo":"red"})
what update loe
2 parameters set in find
query above match. example - "abilities.bar":"beagle"
, "abilities.foo":"red"
, update "loe"
in object .99
.
does mongo have built in function can set value of key key(s) in object equals value? or need create client side function return array index , update
based on that? example:
some function(){...} db.test.update({"abilities.bar":"beagle","abilities.foo":"red"} { $set: { "abilities[x].loe" : .99 } } )
use $elemmatch below :
db.collection.update({"abilities":{"$elemmatch":{"bar":"beagle","foo":"red"}}}, {"$set":{"abilities.$.loe":0.99}})
Comments
Post a Comment