簡體   English   中英

mongo中地理空間數據庫的python等效查詢

[英]equivalent query for python for geospatial db in mongo

我有一個像這樣的mongo數據庫

{ "_id" : ObjectId("55068cf8d77d293a1ca8475a"), 
  "category" : "City", 
  "loc" : { "type" : "Point", 
            "coordinates" : [ 88.789167, 22.407222 ] }, 
            "name" : "rajbari" 
          }
}

我已經創建了這樣的2dsphere索引

db.places.createIndex( { loc : "2dsphere" , category : -1, name: 1 } )

當我在mongo上查詢時,我得到了想要的結果

db.places.find( { loc : { $geoWithin : { $centerSphere :[ [ 88.777778, 22.432778] , 3 / 3959 ] } } } )

這從數據庫獲取了4行。

我需要為此制作一個python api,並編寫了以下代碼

def get_connection():
    connection = Connection()
    db = connection[database_name]
    collection = db[collection_name]
    return collection

central_points = [float(x) for x in request.GET.get('central-points').split(',')]
level = str(request.GET.get('zoom-level'))
places = get_connection()
points = places.find( { "loc" : { "$geoWithin" : { "$centerSphere" :[central_points ,level / 3959 ] } } } )

這僅獲取我一行,當我傳遞central_points內部數據庫中存在的實際坐標時,也是central_points

無法理解我在做什么錯

可能有問題:

level = str(request.GET.get('zoom-level'))

根據您的情況,var level應為int或float。

level = int(request.GET.get('zoom-level'))

嘗試更正該問題,然后再次運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM