【问题标题】:Match Coordinate Array in MongoDB Query在 MongoDB 查询中匹配坐标数组
【发布时间】: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:{...}} 条件数组,具体取决于您的期望 - 匹配文档的交集或并集。
  • 你能给我查询的概述吗?如何实现我的目标

标签: mongodb waterline


【解决方案1】:

问题

它不适用于$near。该函数按距离对结果进行排序,当查询中$near运算符超过1个时,并不清楚按什么顺序返回结果,所以请求like

db.collection.find({$or: [
    {'location':{$near:{$geometry:{type:"point",coordinates:[ 72.50325, 23.01222 ]},$maxDistance:50000}}},
    {'location':{$near:{$geometry:{type:"point",coordinates:[ 72.7788, 23.8787 ]},$maxDistance:50000}}}
]});

https://github.com/mongodb/mongo/blob/3cbb3eb/src/mongo/db/query/canonical_query.cpp#L346 中明确禁止:

// There can only be one NEAR.  If there is a NEAR, it must be either the root or the root
// must be an AND and its child must be a NEAR.
size_t numGeoNear = countNodes(root, MatchExpression::GEO_NEAR);
if (numGeoNear > 1) {
    return Status(ErrorCodes::BadValue, "Too many geoNear expressions");
} 

解决方法

是使用$geoWithin

返回任意坐标(联合)周围50公里半径内的所有文档:

db.collection.find(
    {$or: [
        {'location':{$geoWithin: {$centerSphere:[[ 72.50325, 23.01222 ], 50 / 6378.1]}}},
        {'location':{$geoWithin: {$centerSphere:[[ 72.7788, 23.8787 ], 50 / 6378.1]}}}
    ]}
)

返回两个文档。

返回所有坐标(交点)周围50公里半径内的所有文档:

db.collection.find(
    {$and: [
        {'location':{$geoWithin: {$centerSphere:[[ 72.50325, 23.01222 ], 50 / 6378.1]}}},
        {'location':{$geoWithin: {$centerSphere:[[ 72.7788, 23.8787 ], 50 / 6378.1]}}}
    ]}
)

不返回任何文档,因为它们每个都在自己的圈子内,但是如果您增加距离,例如到 500 公里:

db.collection.find(
    {$and: [
        {'location':{$geoWithin: {$centerSphere:[[ 72.50325, 23.01222 ], 500 / 6378.1]}}},
        {'location':{$geoWithin: {$centerSphere:[[ 72.7788, 23.8787 ], 500 / 6378.1]}}}
    ]}
)

它将返回两个文档。

它们当然不会被排序。

【讨论】:

  • 感谢您的宝贵回答。让我检查一下。
猜你喜欢
  • 2013-11-22
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
  • 2020-11-17
  • 1970-01-01
  • 2017-10-06
  • 2021-06-18
  • 2013-04-08
相关资源
最近更新 更多