/usr/share/doc/python-pymongo-doc/html/_sources/examples/geo.txt is in python-pymongo-doc 2.6.3-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | Geospatial Indexing Example
===========================
.. testsetup::
from pymongo import MongoClient
client = MongoClient()
client.drop_database('geo_example')
This example shows how to create and use a :data:`~pymongo.GEO2D`
index in PyMongo.
.. note:: 2D indexes require server version **>= 1.3.4**. Support for
2D indexes also requires PyMongo version **>= 1.5.1**.
.. mongodoc:: geo
Creating a Geospatial Index
---------------------------
Creating a geospatial index in pymongo is easy:
.. doctest::
>>> from pymongo import MongoClient, GEO2D
>>> db = MongoClient().geo_example
>>> db.places.create_index([("loc", GEO2D)])
u'loc_2d'
Inserting Places
----------------
Locations in MongoDB are represented using either embedded documents
or lists where the first two elements are coordinates. Here, we'll
insert a couple of example locations:
.. doctest::
>>> db.places.insert({"loc": [2, 5]})
ObjectId('...')
>>> db.places.insert({"loc": [30, 5]})
ObjectId('...')
>>> db.places.insert({"loc": [1, 2]})
ObjectId('...')
>>> db.places.insert({"loc": [4, 4]})
ObjectId('...')
Querying
--------
Using the geospatial index we can find documents near another point:
.. doctest::
>>> for doc in db.places.find({"loc": {"$near": [3, 6]}}).limit(3):
... repr(doc)
...
"{u'loc': [2, 5], u'_id': ObjectId('...')}"
"{u'loc': [4, 4], u'_id': ObjectId('...')}"
"{u'loc': [1, 2], u'_id': ObjectId('...')}"
It's also possible to query for all items within a given rectangle
(specified by lower-left and upper-right coordinates):
.. doctest::
>>> for doc in db.places.find({"loc": {"$within": {"$box": [[2, 2], [5, 6]]}}}):
... repr(doc)
...
"{u'loc': [4, 4], u'_id': ObjectId('...')}"
"{u'loc': [2, 5], u'_id': ObjectId('...')}"
Or circle (specified by center point and radius):
.. doctest::
>>> for doc in db.places.find({"loc": {"$within": {"$center": [[0, 0], 6]}}}):
... repr(doc)
...
"{u'loc': [1, 2], u'_id': ObjectId('...')}"
"{u'loc': [4, 4], u'_id': ObjectId('...')}"
"{u'loc': [2, 5], u'_id': ObjectId('...')}"
geoNear queries are also supported using :class:`~bson.son.SON`:
.. doctest::
>>> from bson.son import SON
>>> db.command(SON([('geoNear', 'places'), ('near', [1, 2])]))
{u'ok': 1.0, u'near': u'1100000000000001100111100111100000000001100111100111', u'ns': u'geo_example.places', u'stats': ...}
|