Combine two OR-queries with AND in Mongoose

后端 未结 1 1134
我在风中等你
我在风中等你 2020-12-02 08:35

I want to combine two OR-queries with AND in Monoose, like in this SQL statement:

SELECT * FROM ... WHERE (a = 1 OR b = 1) AND (c=1 OR d=1)

相关标签:
1条回答
  • 2020-12-02 09:13

    It's probably easiest to create your query object directly as:

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

    But you can also use the Query#and helper that's available in recent 3.x Mongoose releases:

      Test.find()
          .and([
              { $or: [{a: 1}, {b: 1}] },
              { $or: [{c: 1}, {d: 1}] }
          ])
          .exec(function (err, results) {
              ...
          });
    
    0 讨论(0)
提交回复
热议问题