【发布时间】:2015-09-25 05:41:57
【问题描述】:
我在 db test 中有许多具有以下结构的文档(由唯一的 toolname 提供):
{
"_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
}
]
}
我可以通过以下查询find此文档:
db.test.find({"abilities.bar":"beagle","abilities.foo":"red"})
我想做的是更新LOE,我在上面的find 查询中设置的两个参数匹配。例如 - 在 "abilities.bar":"beagle" 和 "abilities.foo":"red" 的情况下,将该对象中的 "LOE" 更新为 .99。
Mongo 是否有一个内置函数,您可以在其中设置一个键的值,仅当该对象中的另一个键等于某个值时?还是我需要创建一个客户端函数来返回数组索引和update 基于它?示例:
some function(){...}
db.test.update({"abilities.bar":"beagle","abilities.foo":"red"}
{ $set: { "abilities[x].LOE" : .99 } }
)
【问题讨论】:
标签: javascript mongodb