猫鼬查询:将一个值与同一文档的另一个转换值进行比较



我需要查询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')
}
})

相关内容

最新更新