MongoDB – find result where value is null or less than X

The $or operator could be quite cumbersome at times, especially if you have other fields with multiple options, then you’d have to put all the $or’s nested within an $and.

Another option could be to use the negation operator:

"end_date": {"$not": {"$lt":1376982000}}

This will give you the same results as

$or: [{end_date: null}, {end_date: {$gte: 1376982000}}]

And your query would look like this, which is somewhat cleaner IMO:

program_enrollments.find({
  start_date: {$lte: 1376982000},
  end_date: {$not: {$lt: 1376982000}},
  client:"52002d02cc94a31a0f000000"
})

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)