[英]Update multiple embedded documents in multiple documents in MongoDB
I am trying to update multiple embedded documents in multiple documents in MongoDB using mongoose in nodejs. 我正在尝试使用nodejs中的mongoose更新MongoDB中多个文档中的多个嵌入式文档。
An example of the document is shown below. 该文档的示例如下所示。
As you can see, in a 'exampleDocument' there is an embedded document: 'embededDoc' . 如您所见,在“ exampleDocument”中有一个嵌入式文档: “ embededDoc” 。
let exampleDocument =
{
_id: '1aa',
player_id: '9pp',
docUpdated: false',
embededDoc:
[
{
_id: '3eb',
embededDocUpdated: false,
timeDocWasSaved: '2019-04-30T08:45:50.349Z'
}
],
}
How can I update multiple embedded 'embededDoc' documents in multiple 'exampleDocument' documents. 如何在多个“ exampleDocument”文档中更新多个嵌入式“ embededDoc ”文档。
With the help of this answer I know how to update multiple documents with a single command like you can see below: 借助此答案,我知道如何使用单个命令更新多个文档,如下所示:
db.exampleDocument.update(
{ _id: { $in: ['1aa', '2bb', '3cc'] } },
{ $set: { docUpdated : yes } }
)
What if I wanted to update several embeded documents( embededDoc ) by their ids? 如果我想通过其ID更新多个嵌入文档( embededDoc )怎么办?
For example assuming we have three ' exampleDocument ' documents with the ids: '1aa', '2bb', '3cc'. 例如假设我们有三个“exampleDocument”与IDS文件:“1AA”,“2BB”,“3CC”。 And each of these documents has five embeded documents and I wanted to update some of the embeded documents by their ids... is it possible to do it with one command or will I have to do it document by document?
每个文档都有五个嵌入文档,我想通过其ID更新一些嵌入文档...是否可以使用一个命令来完成,还是必须逐个文档地进行?
If possible I would really appreciate a code example with the answer, thank you. 如果可能的话,我将非常感谢带有答案的代码示例,谢谢。
Try with this 试试这个
db.collection.update(
{ "embededDoc._id": "3eb" },
{ $set: { "embededDoc.$.embededDocUpdated" :true } }
)
You have to use filtered positional update operater $[elem]
, Try below solution, i have tested it with some sample documents given below: 您必须使用过滤的位置更新操作符
$[elem]
,请尝试以下解决方案,我已经使用下面给出的一些示例文档进行了测试:
Solution: 解:
db.test.update({
_id: {
$in: ['1aa', '1bb', '1cc']
}
}, {
$set: {
"embededDoc.$[elem].embededDocUpdated": true,
"docUpdated" : "yes"
}
}, { arrayFilters: [{"elem._id": {$in: ['3ed',"3eg","3eb","3eh"]}}], multi: true})
i have tested above query with below sample Documents and its working fine as you want: 我已经用下面的示例文档测试了上面的查询,并且可以根据需要正常工作:
[{
"_id" : "1cc",
"player_id" : "9pp",
"docUpdated" : false,
"embededDoc" : [
{
"_id" : "3ed",
"embededDocUpdated" : false,
"timeDocWasSaved" : "2019-04-30T08:45:50.349Z"
},
{
"_id" : "3eh",
"embededDocUpdated" : false,
"timeDocWasSaved" : "2019-04-30T08:45:50.349Z"
}
]
},
{
"_id" : "1bb",
"player_id" : "9pp",
"docUpdated" : false,
"embededDoc" : [
{
"_id" : "3ec",
"embededDocUpdated" : false,
"timeDocWasSaved" : "2019-04-30T08:45:50.349Z"
},
{
"_id" : "3eg",
"embededDocUpdated" : false,
"timeDocWasSaved" : "2019-04-30T08:45:50.349Z",
}
]
},
{
"_id" : "1aa",
"player_id" : "9pp",
"docUpdated" : false,
"embededDoc" : [
{
"_id" : "3eb",
"embededDocUpdated" : false,
"timeDocWasSaved" : "2019-04-30T08:45:50.349Z",
},
{
"_id" : "3ef",
"embededDocUpdated" : false,
"timeDocWasSaved" : "2019-04-30T08:45:50.349Z"
}
]
}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.