【问题标题】:Combining not, and, and size结合 not、and 和 size
【发布时间】:2013-05-23 00:29:13
【问题描述】:

MongoDB 有一个由 https://jira.mongodb.org/browse/SERVER-478 描述的奇妙问题(以及更通用的版本 https://jira.mongodb.org/browse/SERVER-589

作为一种解决方法,我试图将我的查询表达为 and,not and size 的组合:

     { '$match' => {
      '$and' => [
        items: {'$and' => ['$size' => 0, '$size' => 1, ... until the length I need is reached]},
        ...
      ],
    }},

我不断收到failed with error 10068: "exception: invalid operator: $and"。我是否忽略了一些非常明显的东西?

【问题讨论】:

    标签: mongodb aggregation-framework


    【解决方案1】:

    首先,如果您尝试使用$and,您可能不会得到任何结果,因为查询将尝试查找其大小应与您提供的所有大小(0,1,...您需要的长度)。我想你应该试试$or。其次,以下是您可能真正需要的:

    db.test.aggregate(
      {$match:{
        $or:[ {items:{$size:0}}, {items:{$size:4}} ]} // just include all your size conditions
      }
    )
    

    【讨论】:

    • 哦,所以我需要在里面重复项目和/或而不是相反?
    • 是的,我想是的。这就是您收到 invalid operator 错误的原因。
    猜你喜欢
    • 2017-07-29
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 2013-05-23
    • 2023-03-22
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    相关资源
    最近更新 更多