简体   繁体   English

mongo中地理空间数据库的python等效查询

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

I have a mongo database like this 我有一个像这样的mongo数据库

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

I have created a 2dsphere index like this 我已经创建了这样的2dsphere索引

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

When I query on mongo, I am getting desired result 当我在mongo上查询时,我得到了想要的结果

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

This fetches me 4 rows from the db. 这从数据库获取了4行。

I need to make a python api for this, and have written the following code 我需要为此制作一个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 ] } } } )

This is fetching me only single row, thats too when I pass actual coordinates present in database inside central_points 这仅获取我一行,当我传递central_points内部数据库中存在的实际坐标时,也是central_points

Not able to understand what I am doing wrong 无法理解我在做什么错

There may be something wrong in: 可能有问题:

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

the var level should be int or float in your situation. 根据您的情况,var level应为int或float。

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

Try correct that and run again. 尝试更正该问题,然后再次运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM