Create a 2d Index
2d indexes support queries on location data in a flat, Euclidean plane.
To create a 2d index, use the db.collection.createIndex()
method. The index type is "2d"
:
db.<collection>.createIndex( { <location field> : "2d" } )
About this Task
The values in the
<location field>
must be legacy coordinate pairs.When specifying legacy coordinate pairs, list the longitude first, and then latitude.
Valid longitude values are between
-180
and180
, both inclusive.Valid latitude values are between
-90
and90
, both inclusive.
Before You Begin
Create the contacts
collection:
db.contacts.insertMany( [ { name: "Evander Otylia", phone: "202-555-0193", address: [ 55.5, 42.3 ] }, { name: "Georgine Lestaw", phone: "714-555-0107", address: [ -74, 44.74 ] } ] )
The address
field contains legacy coordinate pairs.
Procedure
Create a 2d index on the address
field:
db.contacts.createIndex( { address : "2d" } )
Next Steps
After you create a 2d index, you can use your 2d index to support calculations on location data. To see examples of queries that use 2d indexes, see:
Learn More
To create an index that supports calculations on spherical surfaces, see 2dsphere Indexes.