对于以下文档,我想更新 status
数组中特定消息的messages
:
[{
"_id" : ObjectId("6079bab4f297df39a44609cb"),
"title" : "Test messages of single user",
"messages" : [
{
"_id" : ObjectId("6079bab4f297df39a44609cc"),
"body" : "Test 1",
"status" : 1
},
{
"_id" : ObjectId("6079bab4f297df39a44609cd"),
"body" : "Test 2",
"status" : 1
},
{
"_id" : ObjectId("6079bcf7c041b00ec4cebb9d"),
"body" : "Hello I'm Sam",
"status" : 1
}
]
}]
在查询 here 下运行
db.update(
{
"_id": ObjectId("6079bab4f297df39a44609cb"),
"messages": { $elemMatch: { _id: ObjectId("6079bcf7c041b00ec4cebb9d") } }
},
{
$set: { "messages.$.status": 2 }
}
)
在查询 here 下运行
db.update(
{
"_id": ObjectId("6079bab4f297df39a44609cb"),
"messages": { $elemMatch: { _id: ObjectId("6079bcf7c041b00ec4cebb9d") } }
},
[
{
$set: { "messages.status": 2 }
}
]
)
MongoDB 版本:4.4
注意:出于某种原因,我需要使用第二个查询,但我无法更新单个消息的状态。它总是更新所有消息
如果你能分享查询链接会很有帮助
答案 0 :(得分:1)
演示 - https://mongoplayground.net/p/VZHes8--O5O
使用$[]
<块引用>过滤后的位置运算符 $[] 标识与更新操作的 arrayFilters 条件匹配的数组元素
db.collection.update(
{ "_id": ObjectId("6079bab4f297df39a44609cb") },
{ $set: { "messages.$[m].status": 2 } },
{ arrayFilters: [ { "m._id": ObjectId("6079bcf7c041b00ec4cebb9d") } ]
})
更新
演示 - https://mongoplayground.net/p/GyBxksYKpmf
使用$map
db.collection.update({
"_id": ObjectId("6079bab4f297df39a44609cb"),
"messages": {
$elemMatch: {
_id: ObjectId("6079bcf7c041b00ec4cebb9d")
}
}
},
[
{
$set: {
"messages": {
$map: {
input: "$messages",
as: "m",
in: {
$cond: [
{ $eq: [ "$$m._id", ObjectId("6079bcf7c041b00ec4cebb9d") ] }, // condition
{ $mergeObjects: [ "$$m", { status: 2 } ] }, // true
"$$m" // false
]
}
}
}
}
}
])
注意 - 在这种情况下它是矫枉过正