Geospatial Indexing Example

This example shows how to create and use a GEO2D index in PyMongo. To create a spherical (earth-like) geospatial index use GEOSPHERE instead.

See also

The MongoDB documentation on

geo

Creating a Geospatial Index

Creating a geospatial index in pymongo is easy:

  1. >>> from pymongo import MongoClient, GEO2D
  2. >>> db = MongoClient().geo_example
  3. >>> db.places.create_index([("loc", GEO2D)])
  4. 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:

  1. >>> result = db.places.insert_many([{"loc": [2, 5]},
  2. ... {"loc": [30, 5]},
  3. ... {"loc": [1, 2]},
  4. ... {"loc": [4, 4]}])
  5. >>> result.inserted_ids
  6. [ObjectId('...'), ObjectId('...'), ObjectId('...'), ObjectId('...')]

Note

If specifying latitude and longitude coordinates in GEOSPHERE, list the longitude first and then latitude.

Querying

Using the geospatial index we can find documents near another point:

  1. >>> import pprint
  2. >>> for doc in db.places.find({"loc": {"$near": [3, 6]}}).limit(3):
  3. ... pprint.pprint(doc)
  4. ...
  5. {u'_id': ObjectId('...'), u'loc': [2, 5]}
  6. {u'_id': ObjectId('...'), u'loc': [4, 4]}
  7. {u'_id': ObjectId('...'), u'loc': [1, 2]}

Note

If using pymongo.GEOSPHERE, using $nearSphere is recommended.

The $maxDistance operator requires the use of SON:

  1. >>> from bson.son import SON
  2. >>> query = {"loc": SON([("$near", [3, 6]), ("$maxDistance", 100)])}
  3. >>> for doc in db.places.find(query).limit(3):
  4. ... pprint.pprint(doc)
  5. ...
  6. {u'_id': ObjectId('...'), u'loc': [2, 5]}
  7. {u'_id': ObjectId('...'), u'loc': [4, 4]}
  8. {u'_id': ObjectId('...'), u'loc': [1, 2]}

It’s also possible to query for all items within a given rectangle (specified by lower-left and upper-right coordinates):

  1. >>> query = {"loc": {"$within": {"$box": [[2, 2], [5, 6]]}}}
  2. >>> for doc in db.places.find(query).sort('_id'):
  3. ... pprint.pprint(doc)
  4. {u'_id': ObjectId('...'), u'loc': [2, 5]}
  5. {u'_id': ObjectId('...'), u'loc': [4, 4]}

Or circle (specified by center point and radius):

  1. >>> query = {"loc": {"$within": {"$center": [[0, 0], 6]}}}
  2. >>> for doc in db.places.find(query).sort('_id'):
  3. ... pprint.pprint(doc)
  4. ...
  5. {u'_id': ObjectId('...'), u'loc': [2, 5]}
  6. {u'_id': ObjectId('...'), u'loc': [1, 2]}
  7. {u'_id': ObjectId('...'), u'loc': [4, 4]}

geoNear queries are also supported using SON:

  1. >>> from bson.son import SON
  2. >>> db.command(SON([('geoNear', 'places'), ('near', [1, 2])]))
  3. {u'ok': 1.0, u'stats': ...}

Warning

Starting in MongoDB version 4.0, MongoDB deprecates the geoNear command. Use one of the following operations instead.

  • $geoNear - aggregation stage.
  • $near - query operator.
  • $nearSphere - query operator.

Table of Contents

Previous topic

Datetimes and Timezones

Next topic

Gevent

This Page

Quick search