4

I would like to do a complex and/or query with Mongoose. Here is some pseudocode that shows what I'm trying to do:

Find persons where (
  (date_begin == null || date_begin <= today)
  && (date_end == null || date_end >= tomorrow)
)

Here is my failing attempt at real Mongoose code:

  query = Person
    .find()
    .where('date_begin').equals(null).or.where('date_begin').lte(today)
    .where('date_end').equals(null).or.where('date_end').gte(tomorrow)

Any suggestions would be greatly appreciated! (FYI, today and tomorrow are being set fine, it's the Mongoose syntax I am asking about.)

Community
  • 1
  • 1
dylanized
  • 3,765
  • 6
  • 32
  • 44

1 Answers1

9

Dylanized,

You could do the following, by using the mongodb Logical Query Operators directly like this:

  Person.find({
      $and: [
          { $or: [{date_begin: null}, {date_begin: {$lte : today}}] },
          { $or: [{date_end: null}, {date_end: {$gte : tommorow}}] }
      ]
  }, function (err, results) {
      ...
  }
Abdullah Rasheed
  • 3,562
  • 4
  • 33
  • 51