Mongo 2.6.4中的多个$near不起作用。

3

我有一个文档集合,像这样:

{
  ...
  "CurrentLocation" : {
    "type" : "Point",
    "coordinates" : [ 
        -92.48436693078111, 
        35.85223020932276
    ]
  }
  ...
}

我需要使用两个可能的中心点进行"$near"查询:

db.Truck.find({
    "$or": [{
        "DestinationLocation": {
            "$near": {
                "$geometry": {
                    "type": "Point",
                    "coordinates": [-117.256875, 41.856405]
                },
                "$maxDistance": 100000.0
            }
        }
    }, {
        "DestinationLocation": {
            "$near": {
                "$geometry": {
                    "type": "Point",
                    "coordinates": [-112.256875, 40.856405]
                },
                "$maxDistance": 100000.0
            }
        }
    }]
})

Mongo返回了一个错误:

error:
{
"$err" : "Can't canonicalize query: BadValue Too many geoNear expressions",
"code" : 17287
}

除了在应用程序中进行数据联合之外,是否有任何请求使用两个点的$near方法的方法?谢谢。


你的问题现在成为了我的问题。我已经阅读了 anhlc 的答案,但我想知道是否有另一种更简单和更高效的方法获得相同的结果。 - Kasir Barati
2个回答

6

你能提供一个关于你建议的例子吗? - mosquito87
@anhlc,你确定我必须写多个查询吗?难道没有其他解决方案吗? - Kasir Barati
1
@KasirBarati,是的,在最新的mongodb版本中仍然存在限制:https://github.com/mongodb/mongo/blob/master/src/mongo/db/query/canonical_query.cpp#L364 - anhlc

-1

一个使用$or的示例

db.Truck.find({
    "DestinationLocation": {
        "$or":[{
          "$near": {
              "$geometry": {
                  "type": "Point",
                  "coordinates": [-117.256875, 41.856405]
              },
              "$maxDistance": 100000.0
            }
        },{
        "$near": {
            "$geometry": {
                "type": "Point",
                "coordinates": [-112.256875, 40.856405]
            },
            "$maxDistance": 100000.0
          }
        }]

    }

})

嗨@gsa,你是如何编写这样的查询语句的?它会抛出一个错误。我尝试做同样的事情,但我得到了与问题中解释的相同的错误。:( - Kasir Barati

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接