繁体   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