首頁  >  文章  >  資料庫  >  Python MongoDB Spatial Query

Python MongoDB Spatial Query

WBOY
WBOY原創
2016-06-07 17:38:501098瀏覽

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn