使用pymongo中的聚合应用$geoner查找最近的latlong?

2024-10-04 05:21:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我收藏的文件看起来像这样-

{'_id' : 'Delhi1', 'loc' : [28.34242,77.656565] }
{'_id' : 'Delhi2', 'loc' : [27.34242,78.626523] }
{'_id' : 'Delhi3', 'loc' : [25.34242,77.612345] }
{'_id' : 'Delhi4', 'loc' : [28.34242,77.676565] }

我想使用pymongo应用聚合,根据输入latlong查找相关文档。我已经在'loc'上创建了索引。以下是我目前所做的-

^{pr2}$

但这对我不管用?如何正确使用这个?在


Tags: 文件文档idlocpymongopr2latlongdelhi1
2条回答

实际上,我在collection中创建了'2dsphere'索引,要将geonore与2dsphere一起使用,我们需要在管道中指定sphere=True

pipeline = [{'$geoNear':{'near': [27.8787, 78.2342],
                 'distanceField': "distance",
                 'maxDistance' : 2000,
                 'spherical' : True }}]

不需要运算符1)也不需要小括号2)也不需要括号。在

db.mycollection.aggregate([
    {
        $geoNear: {
            near: { coordinates: [ 27.8787 , 78.2342 ] },
            distanceField: "distance",
            maxDistance: 2000,
            spherical: true
        }
    }
])

相关问题 更多 >