Python MongoDB Spatial Query
Python MongoDB Spatial Query //引入Pymongo from pymongo import MongoClient,GEO2D // 链接数据库gis db = MongoClient().gis //创建索引 db.places.create_index([("loc",GEO2D)]) 'loc_2d' //插入数据 db.places.insert({"loc":[120,30]}) ObjectId('52
Python MongoDB Spatial Query
//引入Pymongo
>>> from pymongo import MongoClient,GEO2D
// 链接数据库gis
>>> db = MongoClient().gis
//创建索引
>>> db.places.create_index([("loc",GEO2D)])
'loc_2d'
//插入数据
>>> db.places.insert({"loc":[120,30]})
ObjectId('520e3893421aa91ddc7a8239')
>>> db.places.insert({"loc":[80,39]})
ObjectId('520e38b6421aa91ddc7a823a')
>>> db.places.insert({"loc":[112.25,56]})
ObjectId('520e38de421aa91ddc7a823b')
>>> db.places.insert({"loc":[125.23,56]})
ObjectId('520e3909421aa91ddc7a823c')
//附近查询 limit 查询前三个
>>>for doc in db.places.find({"loc":{"$near":[115.20,35]}}).limit(3):
doc
{'loc': [120, 30], '_id': ObjectId('520e3893421aa91ddc7a8239')} {'loc': [112.25, 56], '_id': ObjectId('520e38de421aa91ddc7a823b')} {'loc': [125.23, 56], '_id': ObjectId('520e3909421aa91ddc7a823c')}
//拉框查询
>>> for doc in db.places.find({"loc":{"$within":{"$box":[[75.23,20.32],[152.23,60]]}}}):
doc
{'loc': [120, 30], '_id': ObjectId('520e3893421aa91ddc7a8239')} {'loc': [125.23, 56], '_id': ObjectId('520e3909421aa91ddc7a823c')} {'loc': [80, 39], '_id': ObjectId('520e38b6421aa91ddc7a823a')} {'loc': [112.25, 56], '_id': ObjectId('520e38de421aa91ddc7a823b')}
//点缓冲区查询
>>> for doc in db.places.find({"loc":{"$within":{"$center":[[120.2,30.3],10]}}}):
doc
{'loc': [120, 30], '_id': ObjectId('520e3893421aa91ddc7a8239')}
//------参考文档
转载请注明出处:
posted on
,上一篇: SQLite3 模糊查询
推荐阅读
-
Python操作mongodb数据库进行模糊查询操作示例
-
Python实现读取SQLServer数据并插入到MongoDB数据库的方法示例
-
mongodb和python交互
-
python将MongoDB里的ObjectId转换为时间戳的方法
-
python根据时间生成mongodb的ObjectId的方法
-
python数据库-MongoDB的基本使用(54)
-
python数据库-mongoDB的高级查询操作(55)
-
python连接mongodb操作数据示例(mongodb数据库配置类)
-
Python访问MongoDB,并且转换成Dataframe的方法
-
python连接mongodb密码认证实例