更新 MongoDB 中精确元素数组中的字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10432677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update field in exact element array in MongoDB
提问by Denis Ermolin
I have a document structured like this:
我有一个结构如下的文档:
{
_id:"43434",
heroes : [
{ nickname : "test", items : ["", "", ""] },
{ nickname : "test2", items : ["", "", ""] },
]
}
Can I $set
the second element of the items
array of the embedded object in array heros
with nickname
"test"
?
可以予$set
所述的第二元件items
嵌入对象的阵列中的阵列heros
与nickname
"test"
?
Result:
结果:
{
_id:"43434",
heroes : [
{ nickname : "test", items : ["", "new_value", ""] }, // modified here
{ nickname : "test2", items : ["", "", ""] },
]
}
回答by AD7six
You need to make use of 2 concepts: mongodb's positional operatorand simply using the numeric index for the entry you want to update.
您需要利用 2 个概念:mongodb 的位置运算符和简单地使用要更新的条目的数字索引。
The positional operator allows you to use a condition like this:
位置运算符允许您使用这样的条件:
{"heros.nickname": "test"}
and then reference the found array entry like so:
然后像这样引用找到的数组条目:
{"heros.$ // <- the dollar represents the first matching array key index
As you want to update the 2nd array entry in "items", and array keys are 0 indexed - that's the key 1.
由于您想更新“items”中的第二个数组条目,并且数组键的索引为 0 - 这就是键 1。
So:
所以:
> db.denis.insert({_id:"43434", heros : [{ nickname : "test", items : ["", "", ""] }, { nickname : "test2", items : ["", "", ""] }]});
> db.denis.update(
{"heros.nickname": "test"},
{$set: {
"heros.$.items.1": "new_value"
}}
)
> db.denis.find()
{
"_id" : "43434",
"heros" : [
{"nickname" : "test", "items" : ["", "new_value", "" ]},
{"nickname" : "test2", "items" : ["", "", "" ]}
]
}
回答by Rubin Porwal
db.collection.update(
{
heroes:{$elemMatch:{ "nickname" : "test"}}},
{
$push: {
'heroes.$.items': {
$each: ["new_value" ],
$position: 1
}
}
}
)
回答by Kathy
This solution works well. Just want to add one point. Here is the structure. I need to find OrderItemId is 'yyy' and update. If the query field in condition is an array, like below "OrderItems.OrderItemId" is array. You can not use "OrderItems.OrderItemId[0]" as operation in the query. Instead, you need to use "OrderItems.OrderItemId" to compare. Otherwise, it can not match one.
此解决方案效果很好。只想补充一点。这是结构。我需要找到 OrderItemId 是 'yyy' 并更新。如果条件中的查询字段是数组,如下所示“OrderItems.OrderItemId”是数组。您不能在查询中使用“OrderItems.OrderItemId[0]”作为操作。相反,您需要使用“OrderItems.OrderItemId”进行比较。否则,它不能匹配一个。
{
_id: 'orderid',
OrderItems: [
{
OrderItemId: ['xxxx'],
... },
{
OrderItemId: ['yyyy'],
...},
]
}
result = await collection.updateOne(
{ _id: orderId, "OrderItems.OrderItemId": [orderItemId] },
{ $set: { "OrderItems.$.imgUrl": imgUrl[0], "OrderItems.$.category": category } },
{ upsert: false },
)
console.log(' (result.modifiedCount) ', result.modifiedCount)
console.log(' (result.matchedCount) ', result.matchedCount)