2d
Indexes¶On this page本页内容
Use a 对存储为二维平面上的点的数据使用2d
index for data stored as points on a two-dimensional plane. 2d
索引。The 2d
index is intended for legacy coordinate pairs used in MongoDB 2.2 and earlier.2d
索引用于MongoDB 2.2及更早版本中使用的传统坐标对。
Use a 在以下情况下使用2d
index if:2d
索引:
For more information on geospatial queries, see Geospatial Queries.有关地理空间查询的详细信息,请参阅地理空间查询。
Starting in MongoDB 4.0, you can specify a 从MongoDB 4.0开始,您可以为key
option to the $geoNear
pipeline stage to indicate the indexed field path to use. $geoNear
管道阶段指定一个key
选项,以指示要使用的索引字段路径。This allows the 这允许$geoNear
stage to be used on a collection that has multiple 2d
index and/or multiple 2dsphere index:$geoNear
阶段用于具有多个2d
索引和/或多个2dsphere
索引的集合:
2d
index and/or multiple 2dsphere index, you must use the key
option to specify the indexed field path to use.2d
索引和/或多个2dsphere
索引,则必须使用键选项指定要使用的索引字段路径。key
, you cannot have multiple 2d
index and/or multiple 2dsphere index since without the key
, index selection among multiple 2d
indexes or 2dsphere
indexes is ambiguous.Note
If you do not specify the key
, and you have at most only one 2d
index index and/or only one 2d
index index, MongoDB looks first for a 2d
index to use. If a 2d
index does not exists, then MongoDB looks for a 2dsphere
index to use.
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. 2d
索引用作分片键。However, you can create a geospatial index on a sharded collection by using a different field as the shard key.但是,可以使用其他字段作为分片键,在分片集合上创建地理空间索引。
The 2d
index supports calculations on a flat, Euclidean plane. The 2d
index also supports distance-only
calculations on a sphere (i.e. $nearSphere
), but for geometric calculations on a sphere (e.g. $geoWithin
), store data as GeoJSON objects and use a 2dsphere
index.
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.
sparse
Property¶2d
indexes are always sparse and ignore the sparse 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.