2dsphere geospatial indexes

A 2dsphere geospatial index supports queries calculating geometries in an earth-like plane. It is more accurate than the simplistic 2d index, and can support both GeoJSON objects and coordinate pairs as input.

Its current version, since MongoDB 3.2, is version 3. It is a sparse index by default, only indexing documents that have a 2dsphere field value. Assuming that we have a location field in our books collection, tracking the home address of the main author of each book, we could create an index on this field as follows:

> db.books.createIndex( { "location" : "2dsphere" } )

The location field needs to be a GeoJSON object, like this one:

location : { type: "Point", coordinates: [ 51.5876, 0.1643 ] }

A 2dsphere index can also be a part of a compound index, as the first field or otherwise:

> db.books.createIndex( { name: 1, location : "2dsphere" } )
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset