【发布时间】:2018-01-19 13:35:24
【问题描述】:
我正在尝试编写一个脚本来执行这里提到的基本 2dsphere 索引操作 2dsphere 使用 pymongo。
我找不到任何例子来解决这个问题,这是我迄今为止的尝试:
from pymongo import GEOSPHERE
client=MongoClient('localhost',27017)
db=client['dbtest']
points=db['points']
points.create_index([("loc",GEOSPHERE)])
points.insert({"loc":[2 5]})
points.insert({"loc":[30,5]})
more points.insert
for doc in points.find({"loc" : {"$near": { "$geometry" : {"type":"Point","coordinates":[1,2]},"$maxDistance":20}}}):
print doc
它给出了错误pymongo.errors.OperationFailure: database error: can't find special index: 2d for: { loc: { $near: { $geometry: { type: "Point", coordinates: [ 1, 2 ] }, $maxDistance: 20 } } }
【问题讨论】:
-
您的尝试正在我的机器上运行,但我已将 maxDistance 设置为一个非常大的数字。您使用的是哪个 MongoDB 版本?我正在使用 MongoDB 2.4.3。