- Indexes >
- Index Concepts >
- Index Types >
- Geospatial Indexes and Queries >
- 2d Indexes
2d Indexes¶
On this page
Use a 2d index for data stored as points on a two-dimensional plane. The 2d index is intended for legacy coordinate pairs used in MongoDB 2.2 and earlier.
Use a 2d index if:
- your database has legacy location data from MongoDB 2.2 or earlier, and
- you do not intend to store any location data as GeoJSON objects.
See the Geospatial Query Operators for the query operators that support geospatial queries.
Considerations¶
The geoNear command and the $geoNear pipeline stage require that a collection have at most only one 2d index and/or only one 2dsphere index whereas geospatial query operators (e.g. $near and $geoWithin) permit collections to have multiple geospatial indexes.
The geospatial index restriction for the geoNear command and the $geoNear pipeline stage exists because neither the geoNear command nor the $geoNear pipeline stage syntax includes the location field. As such, index selection among multiple 2d indexes or 2dsphere indexes is ambiguous.
No such restriction applies for geospatial query operators since these operators take a location field, eliminating the ambiguity.
Do not use a 2d index if your location data includes GeoJSON objects. To index on both legacy coordinate pairs and GeoJSON objects, use a 2dsphere index.
You cannot use a 2d index as a shard key when sharding a collection. However, you can create and maintain a geospatial index on a sharded collection by using a different field as the shard key.
Behavior¶
The 2d index supports calculations on a flat, Euclidean plane. The 2d index also supports distance-only calculations on a sphere, but for geometric calculations (e.g. $geoWithin) on a sphere, store data as GeoJSON objects and use the 2dsphere index type.
A 2d index can reference two fields. The first must be the location field. A 2d compound index constructs queries that select first on the location field, and then filters those results by the additional criteria. A compound 2d index can cover queries.
Points on a 2D Plane¶
To store location data as legacy coordinate pairs, use an array or an embedded document. When possible, use the array format:
loc : [ <longitude> , <latitude> ]
Consider the embedded document form:
loc : { lng : <longitude> , lat : <latitude> }
Arrays are preferred as certain languages do not guarantee associative map ordering.
For all points, if you use longitude and latitude, store coordinates in longitude, latitude order.
sparse Property¶
2d indexes are sparse by default and ignores the sparse: true option. If a document lacks a 2d index field (or the field is null or an empty array), MongoDB does not add an entry for the document to the 2d index. For inserts, MongoDB inserts the document but does not add to the 2d index.
For a compound index that includes a 2d index key along with keys of other types, only the 2d index field determines whether the index references a document.
Thank you for your feedback!
We're sorry! You can Report a Problem to help us improve this page.