【发布时间】:2021-11-03 06:08:30
【问题描述】:
我有三个集合,即Events、News 和FuneralNews。
我还有另一个 Notifications 集合,它只是所有三个集合的组合,并且包含三个集合之一/一个的引用 ID。
我只想获取 Notifcations 的 eventId 或 newsId 或葬礼新闻 ID 字段 isActive 为 true 的那些
EventsSchema:
var EventsSchema = new Schema({
...
isActive: Boolean
});
FuneralNewsSchema:
var FuneralNewsSchema = new Schema({
...
isActive: Boolean
});
NewsSchema:
var NewsSchema = new Schema({
...
isActive: Boolean
});
NotificationSchema:
var NotificationSchema = new Schema({
type: {
type: String,
required: true
},
creationDate: {
type: Date,
default: Date.now
},
eventId: {type: Schema.Types.ObjectId, ref: 'Events'},
newsId: {type: Schema.Types.ObjectId, ref: 'News'},
funeralNewsId: {type: Schema.Types.ObjectId, ref: 'FuneralNews'},
organisationId: {type: Schema.Types.ObjectId, ref: 'Organization'}
});
在我需要检查引用集合的isActive 属性之前,这是我的查询:
let totalItems;
const query = { organisationId: organisationId };
Notification.find(query)
.countDocuments()
.then((count) => {
totalItems = count;
return Notification.find(query, null, { lean: true })
.skip(page * limit)
.limit(limit)
.sort({ creationDate: -1 })
.populate("eventId")
.populate("newsId")
.populate("funeralNewsId")
.exec();
})
.then((notifications, err) => {
if (err) throw new Error(err);
res.status(200).json({ notifications, totalItems });
})
.catch((err) => {
next(err);
});
现在我不知道如何检查三个已填充集合先前填充的 isActive 字段。
【问题讨论】:
标签: mongodb mongoose mongodb-query aggregation-framework mongoose-populate