使用Python中的PyMongo,我尝试在设定的半径内对Origin LatLon和Destination LatLon进行地理空间查询,并打印第一个结果。我在下面拥有的是我认为它会是什么,但我得到一个错误。正确的方法是什么?
法典:
origin = [float(44.8697193), float(13.8414046)]
dest = [float(48.1367203), float(11.576754)]
query = db.collection.find({'origLatLng': {'$within': {'$center': [origin,.75]}}}, {'destLatLng': {'$within': {'$center': [dest,.75]}}})[0]
print query
错误:
pymongo.errors.OperationFailure: database error: Unsupported projection option: $within
现在,如果我只搜索原点,没有 dest,我不会收到有关"$within"的错误。我做错了什么?
我认为你的查询语法有问题。您实际上有两个查询文档,一个用于origLatLng
,另一个用于destLatLng
。我想你的意思是这样做:
query = db.collection.find({'start': {'$within': {'$center': [origin,.75]}}, 'end': {'$within': {'$center': [dest,.75]}}})[0]
您的第二个查询文档被解释为执行投影,这就是您收到带有"不支持的投影选项:$within
"的操作失败的原因。
不确定您正在使用的实际MongoDB版本,但$within
已被弃用,现在您应该使用$geoWithin
:
http://docs.mongodb.org/manual/reference/operator/geoWithin/
另请参阅上面的链接以获取更多选项。我不是Python的专家,但我希望$geoWithin
能解决你的问题。