在$or中运行多个geo $near查询



在查询mongodb数据库时,是否可以在$或语句中执行多个地理查询,如$near或$geoNear ?

我总是得到错误"exception: $or可能不包含'special'查询"

经过一番搜索,我发现了问题https://jira.mongodb.org/browse/SERVER-3984,该问题已关闭并标记为"固定"。

对我来说,似乎仍然有一个错误,当运行多个地理查询。下面是我的测试代码:

db.entry.find({
    $or: [
        {
            "address.geo": {
                $near: { 
                    $geometry : {
                       type : "Point" ,
                       coordinates : [ 9.7043 , 52.3019 ] },
                    $maxDistance : 50000 
                }
            }
        },
        {
            "address.geo": {
                $near: { 
                    $geometry : {
                       type : "Point" ,
                       coordinates : [ 9.6043 , 52.1019 ] },
                    $maxDistance : 50000 
                }
            }
        }
    ]
}).count();

这就是结果:

uncaught exception: count failed: {
    "errmsg" : "exception: $or may not contain 'special' query",
    "code" : 13291,
    "ok" : 0
}

不能在$or子句中使用$near(或$nearSphere):

$or and GeoSpatial Queries

在2.6版更改。

$or支持geospatial子句,对于near从句(near从句包括$nearSphere和$near)。美元或不能在任何其他子句中包含一个near子句

但是,您可以在$or中使用其他地理空间子句,因此,如果您可以重构查询以使用不同的地理空间操作符,则可以使用$or

不能在$or子句和$and子句中使用$near(或$nearSphere)。执行两个查询获取数据

最新更新