【问题标题】:Geospatial querying with Array parameter使用 Array 参数进行地理空间查询
【发布时间】:2015-04-13 13:32:11
【问题描述】:

这可能吗? 我需要运行查询我的集合多个参数,如数组。它可以是坐标或 GeoJSON 数组。没关系。

例如

coordinates: [ [3.45855, 18.154265], [4.13515, 22.181822] ]
GeoJSON: [ 
    {"type":"Point", "coordinates":[3.45855, 18.154265]},
    {"type":"Point", "coordinates":[4.13515, 22.181822]},
    {"type":"Point", "coordinates":[5.45833, 24.154888]}   
]

我知道I cannot use $or operator 有地理空间查询。

但我也不能使用 $in 运算符。如果我的集合是如下示例文档,我如何使用上述数组之一查询我的集合?

{
    "_id" : ObjectId("54db485b72273a96985dafb7"),
    "firstname" : "John",
    "lastname" : "Doe",
    "location" : {
        "geometry" : {
            "type" : "Point",
            "coordinates" : [ 
                40.937066, 
                29.326202
            ]
        }
    }
}

编辑:

实际上,我需要在我的数组查询中使用 $near 和 $maxDistance 运算符。我可以正确地找到一个给定的Point 参数的结果,如下所示。它有效;

{'location.geometry': 
    { 
        $near: 
        {
            $geometry: GeoJSON[0],
            $maxDistance: 500  
        }
    }
}

但我需要如下查询;

{'location.geometry': 
    { 
        $near: 
        {
            $geometry: { $in: GeoJSON },
            $maxDistance: 500  
        }
    }
}

【问题讨论】:

  • 你能描述一下你想用这些点运行什么地理查询吗?这些点是定义一个多边形还是您希望每个点运行一个地理操作?
  • 您好@wdberkeley,我已尝试根据您的问题编辑我的问题。是的,实际上我希望每点运行一次地理操作。有没有机会找到距离上述给定数组坐标近 500 米的文档?
  • 当然可以,但是您需要为每个点运行一个查询。您不能对它们进行批处理或将它们拼凑到一个操作中。
  • 感谢@wdberkeley,希望将来我们可以批量进行地理空间查询:) 我的解决方案对我来说性能很好。

标签: mongodb mongoose mongodb-query


【解决方案1】:

我已经使用async javascript library 解决了这个问题,如下所示;

    var totalCount = 0
            index = 0;

    async.whilst(
        function(){  
            return index < GeoJSON.length 
        },
        function(callback) { // main function
            var geoQuery = myCollection.find();
            geoQuery.where({
                'location.geometry': {
                    $near: {
                        $geometry: GeoJSON[index],
                        $maxDistance: 500
                    }
                }
            })
            .count(function(err, count){
                if(err) callback(err);
                else {      
                    totalCount += count;
                    index++;
                    callback(null)
                }
            })
        },
        function (err){ // callback function
            res.send({ 
                error: err, 
                count: totalCount 
            })
        }
    );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多