【发布时间】:2021-02-21 18:05:33
【问题描述】:
我在查询通知架构时遇到问题
receiver: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Profile' }],
readBy: [{
readerId: { type: mongoose.Schema.Types.ObjectId,
ref: 'Profile', default: [] },
readAt: { type: Date }
}]
为了查询最新的通知,这是我写的查询:
GOAL 是检查 profile.id" DOES NOT 是否存在于 readBy 数组 中(这意味着该用户未阅读它)
const notifications = await Notification.find({
receiver: profile.id, // this works
readBy: { // but, adding this returns an empty array
$elemMatch: {
readerId: { $ne: profile.id }
}
}
})
非常感谢您的帮助,在这里停留了几天。
【问题讨论】:
标签: node.js mongodb express mongoose mongoose-schema