【问题标题】:How can I do a conditional Mongoose query that uses AND and OR? [duplicate]如何进行使用 AND 和 OR 的条件 Mongoose 查询? [复制]
【发布时间】:2016-03-04 09:40:36
【问题描述】:

我想用 Mongoose 做一个复杂的和/或查询。这是一些伪代码,显示了我正在尝试做的事情:

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

这是我对真正 Mongoose 代码的失败尝试:

  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)

任何建议将不胜感激! (仅供参考,今天和明天的设置很好,这是我要问的 Mongoose 语法。)

【问题讨论】:

    标签: javascript node.js mongodb mongoose database


    【解决方案1】:

    染色,

    您可以像这样直接使用 mongodb Logical Query Operators 来执行以下操作:

      Person.find({
          $and: [
              { $or: [{date_begin: null}, {date_begin: {$lte : today}}] },
              { $or: [{date_end: null}, {date_end: {$gte : tommorow}}] }
          ]
      }, function (err, results) {
          ...
      }
    

    【讨论】:

    • 效果很好,非常感谢!
    猜你喜欢
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多