MongoDB provides the following geospatial index types that support geospatial queries.
- 2d indexes support queries that calculate geometries on a two-dimensional plane.
2dsphere
indexes support queries that calculate geometries on an earth-like sphere.
In this article, I create a 2dsphere
index.
Example Collection
Suppose we have a collection called bars
with the following documents:
{ "_id" : 1, "name" : "Boardwalk Social", "location" : { "type" : "Point", "coordinates" : [ 145.77675259719823, -16.919297718553366 ] } } { "_id" : 2, "name" : "The Downunder Bar", "location" : { "type" : "Point", "coordinates" : [ 145.77621640842125, -16.92107838010542 ] } } { "_id" : 3, "name" : "Riley", "location" : { "type" : "Point", "coordinates" : [ 145.7739955395154, -16.916028253292883 ] } } { "_id" : 4, "name" : "Salt House", "location" : { "type" : "Point", "coordinates" : [ 145.78148426655065, -16.91823513430776 ] } } { "_id" : 5, "name" : "Rattle n Hum", "location" : { "type" : "Point", "coordinates" : [ 145.77746095331537, -16.920051942529685 ] } }
Each document has location information stored as a GeoJSON object.
A GeoJSON object has a field named type
that specifies the GeoJSON object type and a field named coordinates
that specifies the object’s coordinates.
Create the 2dsphere Index
Now let’s create the 2dsphere
index.
db.bars.createIndex( { location : "2dsphere" } )
Output:
{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
The 2dsphere
index has now been created.
We can now use the getIndexes()
method to check our index:
db.bars.getIndexes()
Result:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" }, { "v" : 2, "key" : { "location" : "2dsphere" }, "name" : "location_2dsphere", "2dsphereIndexVersion" : 3 } ]
We can see that the index was created as a 2dsphere
index, using 2dsphereIndexVersion
of 3
, which is the default version for my current MongoDB installation (4.4).
Create a Compound 2dsphere Index
You can include 2dsphere
index keys in compound indexes that are combined with non-geospatial index keys.
For example, we could have combined our location
field with the name
field to create a compound index.
Let’s drop the index and create a compound index:
db.bars.dropIndex("location_2dsphere")
db.bars.createIndex( { location : "2dsphere", name : 1 } )
Output:
{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
And check the index:
db.bars.getIndexes()
Result:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" }, { "v" : 2, "key" : { "location" : "2dsphere", "name" : 1 }, "name" : "location_2dsphere_name_1", "2dsphereIndexVersion" : 3 } ]
Compound 2dsphere
indexes can reference multiple location and non-location fields. This is in contrast to compound 2d
indexes, which are limited to referencing just one location field and one other field.
Changing the 2dsphereIndexVersion
You can change the 2dsphereIndexVersion
by adding it as a field with the desired value when creating the index.
Example:
db.bars.createIndex(
{ location : "2dsphere" },
{ "2dsphereIndexVersion" : 2 }
)
Output:
{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
Check the index:
db.bars.getIndexes()
Result:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" }, { "v" : 2, "key" : { "location" : "2dsphere" }, "name" : "location_2dsphere", "2dsphereIndexVersion" : 2 } ]
This index has been created as a 2dsphere
index version 2.
Version 2 is the default version of 2dsphere
indexes created in MongoDB 2.6 and 3.0 series.
Version 3 is the default version of 2dsphere
indexes created in MongoDB 3.2 and later (at the time of writing).
When I created the index without specifying the 2dsphereIndexVersion
field, it created the index using version 3, because that’s the default version for my MongoDB version (4.4).