【问题标题】:Mongoose, Node JS, MongoDB and or query not workingMongoose、Node JS、MongoDB 和/或查询不起作用
【发布时间】:2019-08-24 11:10:02
【问题描述】:

我正在运行 MongoDB v3.6.8、Node v8.12.0 和 Mongoose v5.4.4。

这个纯粹的 OR 查询产生结果:

db.messages.find( { $or : [ { senderId : "5c97ca3eed0cc17e8cfb0486" }, { receiverId : "5c8430effe7df210ee3264bf" } ] })

此 AND OR 查询不返回任何结果:

db.messages.find( { $and : [ { $or : [ { senderId : "5c8430effe7df210ee3264bf" }, { receiverId : "5c97ca3eed0cc17e8cfb0486" } ] }, { $or : [ { senderId : "5c97ca3eed0cc17e8cfb0486" }, { receiverId : "5c8430effe7df210ee3264bf" } ] } ] } )

虽然因为它是一个 AND OR 查询,但 if 应该返回结果,因为一个 OR 查询确实如此。我在这里错过了什么吗?我的 AND OR 查询有问题吗?我直接从这里的 Mongo 文档开始:https://docs.mongodb.com/manual/reference/operator/query/and/

任何帮助将不胜感激!

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    要使查询中的 AND 运算符为真,两个 OR 表达式都必须为真。 第一个表达式可能返回 false。

    【讨论】:

      【解决方案2】:

      对于将来遇到相同问题的任何人,我将 $and & $or 混合在一起,因此正确的查询是:

      db.messages.find( { $or : [ { $and : [ { senderId : "5c8430effe7df210ee3264bf" }, { receiverId : "5c97ca3eed0cc17e8cfb0486" } ] }, { $and : [ { senderId : "5c97ca3eed0cc17e8cfb0486" }, { receiverId : "5c8430effe7df210ee3264bf" } ] } ] } )
      

      【讨论】:

        猜你喜欢
        • 2015-11-08
        • 1970-01-01
        • 2014-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-14
        • 2012-06-09
        相关资源
        最近更新 更多