Mongoose.find() How to query document with property greater than some number OR value is null OR property doesnt exist?

Viewed 414

I have documents like let's say:

{
   "name": "John",
   "age" : 10
},
{
   "name": "Jack",
   "age" : 20
},
{
   "name": "Tom",
   "age" : 30
},
{
   "name": "Lisa"
}

How to construct query .find() to mongoose model querying about age eg.

  • age greater than: 13
  • age less than: 27

OR

  • age property is null

OR

  • age property doesn't exist?

Big thanks in advance for the response!

1 Answers

Try $or condition,

db.collection.find({
  $or: [
    { age: { $gt: 13, $lt: 27 } },
    { age: null },
    { age: { $exists: false } }
  ]
})

Playground

Related