【问题标题】:MongoDB nested query - BadValue errorMongoDB 嵌套查询 - BadValue 错误
【发布时间】:2016-04-05 10:52:58
【问题描述】:

这是我要运行的查询:

{ ownerId: 14,
  '$or': 
   [ { '$or': 
        [ { '$and': 
             [ { provider: 'INSTAGRAM' },
               { tags: 
                  { '$or': 
                     [ { '$in': [ 'skyhotel' ] },
                       { '$or': [ { '$all': ["skyhotel","excellent"] } ] } ] } } ] },
          { '$and': 
             [ { provider: 'VKONTAKTE' },
               { tags: 
                  { '$or': 
                     [ { '$in': [ 'skyhotel' ] },
                       { '$or': [ { '$all': ["skyhotel","excellent"] } ] } ] } } ] } ] },
     { '$or': 
        [ { '$and': 
             [ { provider: 'INSTAGRAM' },
               { authorLogin: { '$in': [ 'valera92', 'petyan' ] } } ] },
          { '$and': 
             [ { provider: 'VKONTAKTE' },
               { authorLogin: { '$in': [ 'valera92' ] } } ] } ] },
     { '$or': 
        [ { '$and': 
             [ { provider: 'INSTAGRAM' },
               { locationId: { '$in': [ '32454234' ] } } ] } ] },
     { '$or': 
        [ { '$and': 
             [ { provider: 'INSTAGRAM' },
               { location: { '$or': [ { '$geoWithin': { '$centerSphere': [ [ '56.829782', '60.593162' ], 0.000012629451881788331 ] } } ] } } ] } ] } ] }

根据 mongo 标准,它似乎格式不正确。我得到的错误是:

Can't canonicalize query: BadValue unknown operator: $or

我可以重新格式化此查询的正确方法是什么?

编辑:文档示例

{
    "_id" : ObjectId("570362332ee1a7ab1ecb9899"),
    "proextid" : "INSTAGRAM_fdfsfsdfsdfwefwef2r3232",
    "updatedAt" : ISODate("2016-04-05T06:58:59.683Z"),
    "createdAt" : ISODate("2016-04-05T06:58:59.683Z"),
    "ownerId" : 7,
    "authorId" : "390599885",
    "authorName" : "name",
    "authorLogin" : "login",
    "authorDetail" : {
        "authorPicture" : "url",
        "authorLink" : "url"
    },
    "externalCreatedAt" : ISODate("2015-08-29T22:42:04.000Z"),
    "externalId" : "fdsfsdfsdfsdfwer2342342423423r23r",
    "detailType" : "PHOTO",
    "location" : [ 
        0, 
        0
    ],
    "locationId" : "",
    "locationTitle" : "",
    "provider" : "INSTAGRAM",
    "detail" : {},
    "description" : "hello",
    "tags" : [ 
        "ленинградскийпроспект", 
        "москва", 
        "архитектура", 
        "историческоенаследие", 
        "петровскийдворец"
    ],
    "commentsCount" : 1,
    "likesCount" : 40,
    "groupName" : "",
    "__v" : 0
}

【问题讨论】:

  • 你想在这里做什么?请提供适当的用例。至少提供集合中的示例文档以及您希望查询找到的内容。
  • 首先,我真的不认为你需要使用 $ 并且无论你在哪里使用它。对象内的每一件事都与 find 方法进行了隐式与运算。
  • @SiddharthAjmera 添加了一个示例文档。不幸的是,摆脱 $ands 并没有帮助。
  • 你想做什么?
  • 写下您希望查询匹配的条件的“单词”可能会更好地解释这一点。您当然不需要所有这些嵌套,并且有太多语法上完全不正确的内容,这使问题更加不清楚。单词和一个“应该匹配”的示例文档和另一个“不应该”的示例文档,其中的单词解释了为什么和为什么不。

标签: javascript mongodb mongoose


【解决方案1】:

您的查询可以简化为:

{
ownerId:14,
provider: { $in: [ 'INSTAGRAM', 'VKONTAKTE' ] },
'$or': [
    {
        tags: { $in: [ 'skyhotel', 'excellent' ] }
    },
    {
        authorLogin: { $in: [ 'valera92', 'petyan' ] },
    },
    {  
        locationId:{ '$in':[ '32454234' ] }
    },
    {  
        location:{  
                    {  
                        '$geoWithin':{
                            '$centerSphere':[ [ '56.829782', '60.593162' ], 0.000012629451881788331 ]
                        }
                    }
                }
    }
]
}

现在这可能不是您想要的。但从您提出的问题以及您提供的信息来看,这是我能给出的最佳建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-01
    • 2021-06-20
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 2021-06-29
    • 2021-02-25
    相关资源
    最近更新 更多