2016-03-11 33 views
2

我的集合中的文件看起來是這樣的 -

{'_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,找出基於輸入經緯度有關文件申請聚集。我創建了'loc'的索引。這是我迄今爲止所做的 -

pipeline = [{'$geoNear':{'near': [27.8787, 78.2342], 
        'distanceField': "distance", 
        'maxDistance' : 2000 }}] 
db['mycollection'].aggregate(pipeline) 

但這不適合我嗎?如何正確使用這個?

回答

2

其實,我創建了收集「2dsphere人指數,並與2dsphere我們需要指定,球形=在管道

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

它看起來像你有幾個格式錯誤,真用geoNear: 1)集合和運算符都不需要括號或括號,2)邏輯運算符是小寫的。

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

沒有,在pymongo,我們需要包裝集合名稱和操作員用戶名在括號內包裝,2 - 在Python邏輯運算符Propercase – userxxx

+0

最值得注意的是這裏的'近:{座標:27.8787,78.2342]} '無效。 ''near''選項可以直接使用座標數組或「GeoJSON對象」{「類型」:「點」,「座標」:[27.8787,78.2342]}。無論如何,@userxxx都回答了自己的問題,增加了「spherical」:True',它在Python中是「布爾值」的正確形式,可以是「True」或「False」。在閱讀問題時注意標籤(不要忘記在問題標題和正文中同時使用pymongo)。 Python與JSON和JavasScript具有「相似」的語法,但有所不同。 –

相關問題