我需要查询paidUntilDate
属性中比今天晚 x 天的日期,其中 x 位于同一文档的属性notificationDaysBefore
中。我正在使用 dayjs 库来查询日期。
我发现我可以使用$expr
比较同一文档中的值,但我不知道我是否可以将其中一个值用作 dayjs.add()
函数的参数并将其与另一个值进行比较。
目前,我有这个查询,它paidUntilDate
从今天起的 7-8 天之间。
const today = dayjs();
await Bed.find({
paidUntilDate: {
$lte: today.add(8, 'days'),
$gte: today.add(7, 'days')
}
})
我只需要将7
替换为notificationDaysBefore
属性中的值,8
替换为7
值 +1。
任何帮助,不胜感激。谢谢!
编辑:我想我最好做这样的事情:
await Bed.find({
}).where(today.diff('paidUntilDate')).in('notificationDaysBefore')
或
await Bed.find({
notificationDaysBefore: today.diff('$paidUntilDate', 'day'),
})
因为notificationDaysBefore
包含一个数字数组,但我仍然没有弄清楚正确的语法。
你可以像这样传递对notificationDaysBefore
的引用
const today = dayjs();
await Bed.find({
paidUntilDate: {
$lte: today.add(8, 'days'),
$gte: today.add('$notificationDaysBefore', 'days')
}
})