I have many documents (by unique toolname
) with the 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
this document with the following query:
db.test.find({"abilities.bar":"beagle","abilities.foo":"red"})
What I would like to do is update the LOE
where the two parameters I set in the find
query from above match. For example - where "abilities.bar":"beagle"
and "abilities.foo":"red"
, update the "LOE"
in that object to .99
.
Does Mongo have a built in function where you can set the value of a key only where another key(s) in that object equals some value? Or do I need to create a client side function to return the array index and update
based on that? Example:
some function(){...}
db.test.update({"abilities.bar":"beagle","abilities.foo":"red"}
{ $set: { "abilities[x].LOE" : .99 } }
)