MongoDB的更新数组元素 [英] MongoDB Update Array element
问题描述
我有一个文件结构,像
{
"_id" : ObjectId("52263922f5ebf05115bf550e"),
"Fields" : [
{
"Field" : "Lot No",
"Rules" : [ ]
},
{
"Field" : "RMA No",
"Rules" : [ ]
}
]
}
我曾尝试使用以下code推入规则阵列,这将持有的对象进行更新。
I have tried to update by using the following code to push into the Rules Array which will hold objects.
db.test.update({
"Fields.Field":{$in:["Lot No"]}
}, {
$addToSet: {
"Fields.Field.$.Rules": {
'item_name': "my_item_two",
'price':1
}
}
}, false, true);
不过,我得到以下错误使用字符串字段名称不能追加到数组[现场]
我该怎么做了更新?我已搜查直通similiar职位,但没有什么是点击。帮助。
How do I do the update? I have searched thru similiar posts but nothing is clicking. Help.
推荐答案
您使用了通配符太深 $
。你匹配在字段
数组中的项,所以你会得到一个访问,有:字段$
。这当然pression返回你的字段
阵列的第一场比赛,所以您可以通过字段。$。现场$ C $达到其领域C>或
字段。$。结果
。
You gone too deep with that wildcard $
. You match for an item in the Fields
array, so you get a access on that, with: Fields.$
. This expression returns the first match in your Fields
array, so you reach its fields by Fields.$.Field
or Fields.$.Result
.
现在,让更新更新
:
db.test.update({
"Fields.Field": "Lot No"
}, {
$addToSet: {
"Fields.$.Rules": {
'item_name': "my_item_two",
'price':1
}
}
}, false, true);
请注意,我已经缩短了查询
,因为它是相当于你的前妻pression。
Please note that I've shortened the query
as it is equal to your expression.
这篇关于MongoDB的更新数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!