【发布时间】:2022-02-18 03:13:43
【问题描述】:
具有以下结构的文档:
{
_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Story' },
notifications: [{
story: {
_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Story' },
video_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Video' }
},
video: {
_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Video' },
video_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Video' }
},
type: { type: String },
read: { type: Number, default: 0 }, // 0 - Unread, 1 - read
ts: { type: Date, default: Date.now }
}]
}
我正在尝试将read 元素等于0 的所有通知标记为已读,但出于任何原因,我的代码只修改了第一个通知:
mongoose.model('User').update({ _id: user_id, 'notifications.read': 0 }, { $set: { 'notifications.$.read': 1 }}, { multi: true });
我是否正确使用了multi: true?
更新
我会说我正在尝试做的事情,使用 placeholder 没有任何意义,因为正如文档所说:
$。充当占位符,在更新中更新与查询条件匹配的第一个元素。
【问题讨论】: