MongoDB:如何更新由数组中的索引引用的数组中的单个子元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11372065/
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
MongoDB: How do I update a single subelement in an array, referenced by the index within the array?
提问by Abe
I'm trying to update a single subelement contained within an array in a mongodb document. I want to reference the field using its array index (elements within the array don't have any fields that I can guarantee will be unique identifiers). Seems like this should be easy to do, but I can't figure out the syntax.
我正在尝试更新 mongodb 文档中包含在数组中的单个子元素。我想使用它的数组索引来引用该字段(数组中的元素没有任何我可以保证将是唯一标识符的字段)。看起来这应该很容易做到,但我无法弄清楚语法。
Here's what I want to do in pseudo-json.
这是我想在伪 json 中做的事情。
Before:
前:
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... old content B ... },
{ ... old content C ... }
]
}
After:
后:
{
_id : ...,
other_stuff ... ,
my_array : [
{ ... old content A ... },
{ ... NEW content B ... },
{ ... old content C ... }
]
}
Seems like the query should be something like this:
似乎查询应该是这样的:
//pseudocode
db.my_collection.update(
{_id: ObjectId(document_id), my_array.1 : 1 },
{my_array.$.content: NEW content B }
)
But this doesn't work. I've spent way too long searching the mongodb docs, and trying different variations on this syntax (e.g. using $slice
, etc.). I can't find any clear explanation of how to accomplish this kind of update in MongoDB.
但这不起作用。我花了很长时间搜索 mongodb 文档,并尝试了这种语法的不同变体(例如 using$slice
等)。我找不到有关如何在 MongoDB 中完成此类更新的任何明确解释。
回答by Abe
As expected, the query is easy once you know how. Here's the syntax, in python:
正如预期的那样,一旦您知道如何进行,查询就很容易了。这是python中的语法:
db["my_collection"].update(
{ "_id": ObjectId(document_id) },
{ "$set": { 'documents.'+str(doc_index)+'.content' : new_content_B}}
)
回答by tomaskazemekas
Update of an array element referenced by an index (e.g. 1 ) in Mongo Shell can also be done by directly indicating the index value:
Mongo Shell 中索引(例如 1 )引用的数组元素的更新也可以通过直接指示索引值来完成:
db.my_collection.update(
{_id : "document_id"},
{$set : {"my_array.1.content" : "New content B"}}
)
回答by Doel
回答by znbwo
db.my_collection.update(
{_id: ObjectId(document_id), my_array : { ... old content A ... } },
{ $set: { "my_array.$.content" : "NEW content B" } }
)
回答by Samuel Cabral
You can use the updateOne function of mongoDB passing the index of the element in array, if the key of old content Bis "value" per example:
您可以使用mongoDB的updateOne函数传递数组中元素的索引,如果每个示例旧内容B的键是“值”:
[
...
"value" : "old content A"
"value" : "old content B"
"value" : "old content C"
...
]
the command should be like this:
命令应该是这样的:
db.collection.updateOne({"_id" : "...,"},{$set: {"my_array.1.value": "NEW content B"}})
回答by Ismael Soschinski
A neat way to do it in Javascript, with backticks, is:
在 Javascript 中使用反引号的一种巧妙方法是:
const index = 1;
... { $set: { [`myArray.${index}.value`]: "new content"} }, ...
回答by iamcrypticcoder
When it's required to update an array element without knowing it's actual index but having a unique identifier of the element:
当需要更新数组元素而不知道它的实际索引但具有元素的唯一标识符时:
// Modify a comment in a bucket
db.POST_COMMENT.update(
{
"_id": ObjectId("5ec424a1ed1af85a50855964"),
"bucket.commentId": "5eaf258bb80a1f03cd97a3ad_lepf4f"
},
{
$set: {
"bucket.$.text": "Comment text changed",
"bucket.$.createdDate": ISODate("2015-12-11T14:12:00.000+0000")
}
}
)
Here "bucket.commentId"
is the unique identifier of an array element.
这"bucket.commentId"
是数组元素的唯一标识符。