0

I need to query dates in the paidUntilDate property that are x days later than today, where x is in the property notificationDaysBefore of the same document. I'm using the dayjs library to query dates.

I found that I could compare values in the same document using $expr, but I don't know if I could use one of the values as a parameter to a dayjs .add() function and compare it to the other value.

Currently I have this query that gets paidUntilDate dates that are between 7-8 days from today.

const today = dayjs();
await Bed.find({
    paidUntilDate: {
      $lte: today.add(8, 'days'),
      $gte: today.add(7, 'days')
    }
  })

I just need to replace the 7 with the values in the notificationDaysBefore property, and 8 with the 7 value +1.

Any help is appreciated. Thanks!

EDIT: I think I'm better off doing something like this:

await Bed.find({
}).where(today.diff('paidUntilDate')).in('notificationDaysBefore')

or

await Bed.find({
  notificationDaysBefore: today.diff('$paidUntilDate', 'day'),
})

since notificationDaysBefore contains an array of numbers, but I still haven't figured out the correct syntax.

edrichhans
  • 113
  • 1
  • 2
  • 11

1 Answers1

0

You can pass reference to notificationDaysBefore like this

const today = dayjs();
await Bed.find({
    paidUntilDate: {
      $lte: today.add(8, 'days'),
      $gte: today.add('$notificationDaysBefore', 'days')
    }
  })
Sarfraaz
  • 1,273
  • 4
  • 15