【问题标题】:Mongo error - Can't canonicalize query: BadValue Unsupported projection optionMongo 错误 - 无法规范化查询:BadValue 不支持的投影选项
【发布时间】:2019-06-19 14:23:54
【问题描述】:

我是 MongoDB 新手,正在尝试执行查询。我有一个公司集合和公司 ID 数组。我想得到attributes.0.ccode 存在且attributes.0.ccode 不为空的结果,并将在数组中提供的ID 中进行检查(cdata

var query = Company.find({ _id: { $in: cdata } },{ "attributes.0.ccode": { $exists: true }, $and: [ { "attributes.0.ccode": { $ne: "" } } ] }).select({"attributes": 1}).sort({});

我得到的错误是

"$err": "Can't canonicalize query: BadValue Unsupported projection option: attributes.0.ccode: { $exists: true }",
"code": 17287

我认为这是一个括号问题,但不知道在哪里。

非常感谢任何帮助。

【问题讨论】:

    标签: arrays mongodb mongodb-query


    【解决方案1】:

    在您的代码中,{ _id: { $in: cdata } } 被解释为查询,而其他所有内容,从 ,{ "attributes.0.ccode": { $e.. 开始作为投影(要显示的字段)。尝试重构您的代码,使_id: {$in ...} 和查询的其余部分属于同一个更高级别的对象。像这样的:

    var query = Company.find({
      _id: {
        $in: cdata
      },
      "attributes.0.ccode": {
        $exists: true
      },
      $and: [
        {
          "attributes.0.ccode": {
            $ne: ""
          }
        }
      ]
    }).select({"attributes": 1}).sort({});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-11
      • 2015-11-08
      • 2014-07-01
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      相关资源
      最近更新 更多