2d
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:2d
索引和/或多个2dsphere
索引的集合上使用$geoNear
阶段:
2d
index and/or multiple 2dsphere index, you must use the key
option to specify the indexed field path to use.2d
索引和/或多个2dsphere
索引,则必须使用key
选项指定要使用的索引字段路径。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
索引中选择索引是不明确的。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 如果您的位置数据包含GeoJSON对象,请勿使用2d
index if your location data includes GeoJSON objects. 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.但是,您可以使用不同的字段作为分片键,在分片集合上创建地理空间索引。
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
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 对于插入,MongoDB插入文档,但不添加到2d
index.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
索引字段确定索引是否引用文档。
2d
indexes only support simple binary comparison and do not support the collation option.索引只支持简单的二进制比较,不支持collation
选项。
To create a 要在具有非简单排序规则的集合上创建2d索引,必须在创建索引时显式指定2d
index on a collection that has a non-simple collation, you must explicitly specify {collation: {locale: "simple"} }
when creating the index.{collation: {locale: "simple"} }
。