Node.js Mongoose查询:将同一文档的一个值与另一个转换后的值进行比较
我需要在Node.js Mongoose查询:将同一文档的一个值与另一个转换后的值进行比较,node.js,mongodb,mongoose,dayjs,Node.js,Mongodb,Mongoose,Dayjs,我需要在paidUntilDate属性中查询比今天晚x天的日期,其中x在同一文档的notificationDaysBefore属性中。我正在使用库查询日期 我发现我可以使用$expr比较同一文档中的值,但我不知道是否可以使用其中一个值作为dayjs.add()函数的参数,并将其与另一个值进行比较 目前,我有一个查询,它获取从今天起7-8天之间的paidUntilDate日期 const today = dayjs(); await Bed.find({ paidUntilDate: {
paidUntilDate
属性中查询比今天晚x天的日期,其中x在同一文档的notificationDaysBefore
属性中。我正在使用库查询日期
我发现我可以使用$expr
比较同一文档中的值,但我不知道是否可以使用其中一个值作为dayjs.add()
函数的参数,并将其与另一个值进行比较
目前,我有一个查询,它获取从今天起7-8天之间的paidUntilDate
日期
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')
或
因为前面的
notificationDaysBefore
包含一个数字数组,但我仍然没有找到正确的语法。您可以像这样传递对notificationDaysBefore
的引用
const today = dayjs();
await Bed.find({
paidUntilDate: {
$lte: today.add(8, 'days'),
$gte: today.add('$notificationDaysBefore', 'days')
}
})
如果$notificationDaysBefore是数组呢?那也行吗?不行。当使用它的数组时,您期望的行为是什么?我认为我最好执行类似于
Bed.find({notificationDaysBefore:today.diff('$paidUntilDate',day')})之类的操作或类似的操作,但我还没有找到正确的语法。
const today = dayjs();
await Bed.find({
paidUntilDate: {
$lte: today.add(8, 'days'),
$gte: today.add('$notificationDaysBefore', 'days')
}
})