How to combine two OR-queries with AND in Mongoose?

Sometimes, we want to combine two OR-queries with AND in Mongoose.

In this article, we’ll look at how to combine two OR-queries with AND in Mongoose.

How to combine two OR-queries with AND in Mongoose?

To combine two OR-queries with AND in Mongoose, we can use the find method.

For instance we write

Test.find({
  $and: [{
      $or: [{
        a: 1
      }, {
        b: 1
      }]
    },
    {
      $or: [{
        c: 1
      }, {
        d: 1
      }]
    }
  ]
}, (err, results) => {
  //...
})

to call Test.find with the $and property set to an array with 2 objects with $or properties.

This combines the 2 OR clauses with AND.

We get the query results from results.

Conclusion

To combine two OR-queries with AND in Mongoose, we can use the find method.