[英]Error while executing geo spatial query in MongoDB using Python
I have just started working with Geo Spatial data using MongoDB and Python.我刚刚开始使用 MongoDB 和 Python 处理地理空间数据。 Below is my code.
下面是我的代码。 For normal queries it works fine but for 'near' query I am getting error.
对于普通查询,它工作正常,但对于“近”查询,我收到错误。
from pymongo import MongoClient
client = MongoClient("mongodb+srv://dummyuser:dummypassword@cluster0.xprs.mongodb.net/test?retryWrites=true&w=majority")
db = client["MyDB"]
col = db["MyCollection"]
query = """{
geometry: {
$near: {
$geometry: {
type: 'Point',
coordinates: [77.25467696734609,28.63449787747656]
},
$maxDistance: 5000,
$minDistance: 10
}
}
}"""
docs = (col.find(query))
for doc in docs:
print(doc)
While executing this code I am getting below error执行此代码时,我遇到以下错误
C:\user\Python\parse-payment-response\venv\Scripts\python.exe C:/user/Python/jsonParser/mongoclnt.py
Traceback (most recent call last):
File "C:/user/Python/jsonParser/mongoclnt.py", line 24, in <module>
docs = [(col.find(query))]
File "C:\user\Python\parse-payment-response\venv\lib\site-packages\pymongo\collection.py", line 1523, in find
return Cursor(self, *args, **kwargs)
File "C:\user\Python\parse-payment-response\venv\lib\site-packages\pymongo\cursor.py", line 144, in __init__
validate_is_mapping("filter", spec)
File "C:\user\Python\parse-payment-response\venv\lib\site-packages\pymongo\common.py", line 494, in validate_is_mapping
raise TypeError("%s must be an instance of dict, bson.son.SON, or "
TypeError: filter must be an instance of dict, bson.son.SON, or any other type that inherits from collections.Mapping
Please help请帮忙
After lots of effort and search I got this fixed.经过大量的努力和搜索,我得到了这个修复。 Code as below:
代码如下:
from pymongo import MongoClient
MongoClient("mongodb+srv://dummyuser:dummypassword@cluster0.xprs.mongodb.net/test?retryWrites=true&w=majority")
db = client["MyDB"]
col = db["MyCollection"]
query={"geometry": {"$nearSphere": {"$geometry": {"type": "Point", "coordinates": [77.25467696734609, 28.63449787747656]}, "$maxDistance": 2000,"$minDistance": 10}}}
docs = col.find(query)
for doc in docs:
print(doc)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.