是否可以通过一次更新来更新文档的两个子数组(无需检索文档,并使用乐观并发更新它)?
假设我有以下文件:
{ "_id" : 2,
"arr1" : [
{ "Id" : 10, "Value" : "abc" },
{ "Id" : 20, "Value" : "abc" },
{ "Id" : 30, "Value" : "abc" },
{ "Id" : 40, "Value" : "abc" },
{ "Id" : 50, "Value" : "abc" }
],
"arr2" : [
{ "Id" : 12, "Text" : "abc" },
{ "Id" : 22, "Text" : "abc" },
{ "Id" : 32, "Text" : "abc" },
{ "Id" : 42, "Text" : "abc" },
{ "Id" : 52, "Text" : "abc" }
]
}
我试图这样做,但它似乎没有修改正确的子文档:
db.col.update(
{ "_id": 2, arr1: { $elemMatch: { Id: 30 } }, arr2: { $elemMatch: {Id: 42} } },
{ $set: {
"arr1.$.Value": "Changed First",
"arr2.$.Text": "Changed Second"
}
})