【发布时间】:2018-06-01 10:15:30
【问题描述】:
我有纬度和经度数组,但我无法匹配整个数组。所以我必须迭代循环并在循环中执行以下单个查询。所以执行查询需要很长时间。
我的以下循环查询
Collection.find({'location':{$near:{$geometry:{type:"point",coordinates:[longitude,latitude]},$maxDistance:20}}}).exec(function(err,res){
});
期待查询喜欢
{'location':{$near:{$geometry:{type:"point",coordinates:[[longitude,latitude],[longitude,latitude],[longitude,latitude]]},$maxDistance:20}}}
我有坐标数组,但我不能在这里传递整个数组,所以我必须在循环中传递单个纬度和经度。
有没有办法使用坐标数组从数据库中查找数据?
更新
我尝试了以下查询
Collection.find({'location':{$near:{$geometry:{type:"point",coordinates:[[72.502912,23.011787],[ 72.50265, 23.011772 ]]},$maxDistance:20}}});
我收到以下错误
error: {
"waitedMS" : NumberLong(0),
"ok" : 0,
"errmsg" : "invalid point in geo near query $geometry argument: { type: \"point\", coordinates: [ [ 72.50291199999999, 23.011787 ], [ 72.50265, 23.011772 ] ] } Point must only contain numeric elements",
"code" : 2
}
文档
{
"_id" : ObjectId("5abcf7ae59869428b40c5727"),
"latitude" : 23.8787,
"longitude" : 72.7788,
"location" : [
72.7788,
23.8787
],
"address" : "Ahmedabad",
"status" : "active",
"eventId" : "5a8d6a27733b28295db9635e",
"eventCreateBy" : "admin",
"description" : "",
"isDeleted" : "0",
"createdAt" : ISODate("2018-03-29T14:26:54.198Z"),
"updatedAt" : ISODate("2018-03-29T14:26:54.198Z")
}
坐标
[72.7788,23.8787] : Matched
[72.5032552,23.0122194]: Matched
[18.1519461,43.8984328]: Not Matched
【问题讨论】:
-
如果有人不清楚我在问什么,请在这里评论。我会解释我想要什么。
-
是的,请解释为什么不能使用数组。
-
我在使用
coordinates:[[longitude,latitude],[longitude,latitude],[longitude,latitude]]时出错 -
啊,很公平。这是错误的语法。它应该是包含在
$and、$or或两者组合中的{$near:{...}}条件数组,具体取决于您的期望 - 匹配文档的交集或并集。 -
你能给我查询的概述吗?如何实现我的目标