我只想得到与req.query中请求的状态、from_date和to_date相匹配的用户的通知。
用户架构:
const userSchema = mongoose.Schema({
email: {
type: String,
required: true
},
password: {
type: String,
minlength: 8,
required: true
},
notifications: [{
notification: {
type: mongoose.Schema.Types.ObjectId,
ref: "Notification"
},
status: {
type: String,
enum: ["Not Acknowledged", "Acknowledged"],
default: "Not Acknowledged"
},
}]
})
字符串
通知架构:
const notificationSchema = mongoose.Schema({
description: {
type: String
},
remind_on: {
type: Date
},
remind_at: {
type: Date
},
});
型
我试过了,但它会发出所有通知。
const statusQuery = {};
if(status) {
statusQuery.match = { 'notifications.status': status }
}
const searchNotifications = await User.findById(user._id)
.populate({
path: "notifications",
...statusQuery,
populate: {
path: "notification",
populate: [
{ path: "assignee", select: "first_name last_name" },
{ path: "case", select: "case_id" },
],
},
})
.select("notifications");
型
在上面的代码中,我首先通过用户ID获取用户文档,并尝试在populate中添加查询
1条答案
按热度按时间rdrgkggo1#
要根据您的问题根据状态、from_date和to_date过滤通知,您需要根据
from_date
和to_date
指定的日期范围内的remind_on
或remind_at
字段为通知添加过滤器。这里有一个同样的例子。
字符串