我有一个具有以下属性的文档:
{'_id':1,
'longitude':1.1 //double,
'latitude':1.2 //double}
}
我想使用pymongo
添加2D geo index
我有以下代码:
collection.create_index([('loc', pymongo.GEO2D)])
data = collection.update_many({},
{'$set':{'loc':['$latitude','$longitude']}})
但我得到的结果是一个错误
pymongo.errors.OperationFailure: location object expected, location array not in correct format
这是因为我插入了两个字符串和“$latitude”,“$longitude”。如何插入纬度和经度的实际值
非常感谢您的帮助
更新:我知道我可以用for循环完成它。但我想避免这样做,因为这将是非常缓慢的
$set
操作似乎不支持Collection.update
上的2D indexes2D索引是遗留的,可能不会添加对
$set
的支持使用聚合修复当前问题,如下所示:
<>我强烈建议考虑使用^ a2}索引。2Dsphere
支持所有地理空间查询,从长远来看,这对您的用例可能是有益的相关问题 更多 >
编程相关推荐