简体   繁体   English

如何在PyMongo中将$ geoNear与聚合一起使用

[英]How to use $geoNear with aggregate in PyMongo

Imagine that you have a collection with the following structure: 假设您有一个具有以下结构的集合:

{
    "_id": ObjectId("543f1ec50204444c53ba39a0"), 
    "request_ip": "61.111.36.11", 
    "owner": ObjectId("543f227c0204444c53ba4b28"),
    "loc": [-116.199, 43.6186]
}

Where loc contains the location of a geographical point defined by its latitude and longitude respectively. 其中loc包含一个地理点的位置,分别由其纬度和经度定义。

Well, what you need is to find the documents in the collection based on the distance calculated between your locations and another given point. 好吧,您需要的是根据位置与另一个给定点之间的距离计算出集合中的文档。

In this case, the aggregate () function is used: 在这种情况下,将使用aggregate ()函数:

cercanos = collection.aggregate(
[{ 
    "$geoNear": {
        "near": [ 52.15077 , 9.95112 ],
        "distanceField": "dist", 
        "spherical": True,
        "limit":2
    }
}])

When you follow the MongoDB documentation ( https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear ) everything seems to be in order, however when you run several errors appear: 当您遵循MongoDB文档( https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear )时,一切似乎都井然有序,但是,当您运行时会出现一些错误:

  Traceback (most recent call last):
  File "E:\Documents\EclipseProjects\Bonus8\src\pru.py", line 154, in <module>
    "spherical": True,
  File "D:\Python\lib\site-packages\pymongo\collection.py", line 1870, in aggregate
    collation=collation)
  File "D:\Python\lib\site-packages\pymongo\collection.py", line 232, in _command
    collation=collation)
  File "D:\Python\lib\site-packages\pymongo\pool.py", line 419, in command
    collation=collation)
  File "D:\Python\lib\site-packages\pymongo\network.py", line 116, in command
    parse_write_concern_error=parse_write_concern_error)
  File "D:\Python\lib\site-packages\pymongo\helpers.py", line 210, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: geoNear command failed: { ok: 0.0, errmsg: "no geo indices for geoNear" }

What happen: 发生什么事:

The problem is that the usage examples provided by the MongoDB reference manual is specific to the exclusive execution within your console ( mongo shell ). 问题在于,MongoDB参考手册提供的用法示例特定于控制台内的独占执行( mongo shell )。 This causes significant changes to be made in the PyMongo environment, such as having to quote the variables. 这会导致在PyMongo环境中进行重大更改,例如必须引用变量。

Possible solution: 可能的解决方案:

Create a Geospatial Index: 创建地理空间索引:

from pymongo import GEO2D
collection.create_index([("loc", GEO2D)])

This specifies a 2-dimensional geospatial index and solves the previously occurring error. 这指定了二维地理空间索引并解决了先前发生的错误。

Output: 输出:

{
    "_id": ObjectId("543f1ec50204444c53ba39a0"), 
    "request_ip": "61.111.36.11", 
    "owner": ObjectId("543f227c0204444c53ba4b28"),
    "loc": [-116.199, 43.6186]
    "dist": 2.18848713
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM