March 2019
Intermediate to advanced
394 pages
11h 4m
English
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 ...
Read now
Unlock full access