【发布时间】:2019-05-13 01:23:05
【问题描述】:
messages: [
{
user: "5c57c85a9354fa24ad749137",
text: "hello",
unread: true
},
{
user: "5c57c85a9354fa24ad749137",
text: "world",
unread: true
}
]
我正在尝试使用 $[] 更新 id 与给定 id 不匹配但没有任何反应的所有数组元素
ChatSchema.update(
{_id: mongoose.Types.ObjectId(req.body.conversationId), "messages": {$elemMatch: {"user": {$ne: mongoose.Types.ObjectId(req.body.userId)}}}},
{$set: {"messages.$[].unread": false}},
{multi: true}
).exec()
当我使用单个 $ 运算符时,它只会更新找到的第一个元素。
我正在使用 mongo 4.0.6 版和 mongoose 6.7.0 版
【问题讨论】: