【发布时间】:2016-10-03 09:44:53
【问题描述】:
我有一个像这样的对象的集合
{
"_id" : ObjectId("5742be02289512cf98bf63e3"),
"name" : "test1",
"attributes" : [
{
"name" : "x",
"color" : "0xd79c9c",
"_id" : ObjectId("5742be02289512cf98bf63e8")
},
{
"name" : "y",
"color" : "0xd79c9c",
"_id" : ObjectId("5742be02289512cf98bf63e7")
},
{
"name" : "z",
"color" : "0xd79c9c",
"_id" : ObjectId("5742be02289512cf98bf63e6")
}
],
"__v" : 6
}
我想更新所有文档,并为每个属性设置新字段。 所以我想运行一个查询来一次更新所有文档。我认为,这个查询会做
db.spaces.update({}, { $set: { "attributes.0.weight": 2 } }, {multi: true})
但是当我运行这个查询时,我得到一个错误:
“代码”:16837,
"errmsg" : "位置操作符没有找到 查询所需的匹配项。未扩展更新:attributes.$.weight"
我不明白为什么。 请帮忙
【问题讨论】:
-
您需要在查询文档中包含数组字段才能使用位置运算符。
-
@chridam,我该怎么做?