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.有关地理空间查询的详细信息,请参阅地理空间查询

Considerations考虑事项

Starting in MongoDB 4.0, you can specify a key option to the $geoNear pipeline stage to indicate the indexed field path to use. 从MongoDB 4.0开始,您可以为$geoNear管道阶段指定一个key选项,以指示要使用的索引字段路径。This allows the $geoNear stage to be used on a collection that has multiple 2d index and/or multiple 2dsphere index:这允许在具有多个2d索引和/或多个2dsphere索引的集合上使用$geoNear阶段:

  • If your collection has multiple 2d index and/or multiple 2dsphere index, you must use the key option to specify the indexed field path to use.如果集合具有多个2d索引和/或多个2dsphere索引,则必须使用key选项指定要使用的索引字段路径。
  • If you do not specify the 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.如果不指定key,则不能有多个2d索引和/或多个2dsphere索引,因为如果没有键,则在多个2d索引或2dsphere索引中选择索引是不明确的。
Note注意

If you do not specify the key, and you have at most only one 2d index and/or only one 2dsphere index, MongoDB looks first for a 2d index to use. 如果您没有指定key,并且您最多只有一个2d索引和/或只有一个2dsphere索引,MongoDB将首先查找要使用的2d索引。If a 2d index does not exists, then MongoDB looks for a 2dsphere index to use.如果2d索引不存在,则MongoDB将查找要使用的2dsphere索引。

Do not use a 2d index if your location data includes GeoJSON objects. 如果您的位置数据包含GeoJSON对象,请勿使用2d索引。To index on both legacy coordinate pairs and GeoJSON objects, use a 2dsphere index.要对传统坐标对GeoJSON对象进行索引,请使用2dsphere索引。

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.但是,您可以使用不同的字段作为分片键,在分片集合上创建地理空间索引。

Behavior行为

The 2d index supports calculations on a flat, Euclidean plane. 2d索引支持在平坦的欧几里得平面上进行计算。The 2d index also supports distance-onlycalculations 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.2d索引还支持球体上的仅距离计算(即$nearSphere),但对于球体上的几何计算(如$geoWithin),请将数据存储为GeoJSON对象并使用2dsphere索引。

A 2d index can reference two fields. 2d索引可以引用两个字段。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. 2d复合索引构造查询,首先在location字段上进行选择,然后根据附加条件筛选这些结果。A compound 2d index can cover queries.复合2d索引可以覆盖查询。

sparse Property属性

2d indexes are always sparse and ignore the sparse option. 2d索引总是稀疏的,忽略sparse选项。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. 如果文档缺少2d索引字段(或者该字段为null或空数组),MongoDB不会将该文档的条目添加到2d索引中。For inserts, MongoDB inserts the document but does not add to the 2d index.对于插入,MongoDB插入文档,但不添加到2d索引。

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.对于包含2d索引键和其他类型键的复合索引,只有2d索引字段确定索引是否引用文档。

Collation Option排序规则选项

2d indexes only support simple binary comparison and do not support the collation option.索引只支持简单的二进制比较,不支持collation选项。

To create a 2d index on a collection that has a non-simple collation, you must explicitly specify {collation: {locale: "simple"} } when creating the index.要在具有非简单排序规则的集合上创建2d索引,必须在创建索引时显式指定{collation: {locale: "simple"} }

←  Query a 2dsphere IndexCreate a 2d Index →