2dsphere Indexes索引

On this page本页内容

Overview概述

A 2dsphere index supports queries that calculate geometries on an earth-like sphere. 2dsphere索引支持计算类地球球体上几何图形的查询。2dsphere index supports all MongoDB geospatial queries: queries for inclusion, intersection and proximity. 索引支持所有MongoDB地理空间查询:包含、交集和邻近查询。For more information on geospatial queries, see Geospatial Queries.有关地理空间查询的详细信息,请参阅地理空间查询

The 2dsphere index supports data stored as GeoJSON objects and legacy coordinate pairs (See also 2dsphere Indexed Field Restrictions). 2dsphere索引支持存储为GeoJSON对象传统坐标对的数据(另请参见2dsphere索引化字段约束)。For legacy coordinate pairs, the index converts the data to GeoJSON Point.对于传统坐标对,索引将数据转换为GeoJSONPoint

Versions版本

2dsphere Index Version索引版本Description描述
Version 3MongoDB 3.2 introduces a version 3 of 2dsphere indexes. MongoDB 3.2引入了2dsphere索引的版本3。Version 3 is the default version of 2dsphere indexes created in MongoDB 3.2 and later. 版本3是在MongoDB 3.2及更高版本中创建的2dsphere索引的默认版本。
Version 2MongoDB 2.6 introduces a version 2 of 2dsphere indexes. MongoDB 2.6引入了2dsphere索引的版本2。Version 2 is the default version of 2dsphere indexes created in MongoDB 2.6 and 3.0 series. 版本2是在MongoDB 2.6和3.0系列中创建的2dsphere索引的默认版本。

To override the default version and specify a different version, include the option { "2dsphereIndexVersion": <version> } when creating the index.要覆盖默认版本并指定其他版本,请在创建索引时包含选项{ "2dsphereIndexVersion": <version> }

sparse Property属性

Version 2 and later 2dsphere indexes are always sparse and ignore the sparse option. 版本2和更高版本的2dsphere索引始终是稀疏的,并忽略sparse选项。If a document lacks a 2dsphere index field (or the field is null or an empty array), MongoDB does not add an entry for the document to the index. 如果文档缺少2dsphere索引字段(或者该字段为null或空数组),MongoDB不会将该文档的条目添加到索引中。For inserts, MongoDB inserts the document but does not add to the 2dsphere index.对于插入,MongoDB会插入文档,但不会添加到2dsphere索引中。

For a compound index that includes a 2dsphere index key along with keys of other types, only the 2dsphere index field determines whether the index references a document.对于包含2dsphere索引键和其他类型键的复合索引,只有2dsphere索引字段确定索引是否引用文档。

Earlier versions of MongoDB only support 2dsphere (Version 1) indexes. 2dsphere (Version 1) indexes are not sparse by default and will reject documents with null location fields.早期版本的MongoDB仅支持2dsphere(版本1)索引。2dsphere(版本1)索引在默认情况下不稀疏,将拒绝location字段为null的文档。

Additional GeoJSON Objects其他GeoJSON对象

Version 2 and later 2dsphere indexes includes support for additional GeoJSON object: MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection. 版本2和更高版本的2dsphere索引包括对附加GeoJSON对象的支持:MultiPointMultiLineStringMultiPolygonGeometryCollectionFor details on all supported GeoJSON objects, see GeoJSON Objects.有关所有支持的GeoJSON对象的详细信息,请参阅GeoJSON对象

Considerations注意事项

geoNear and $geoNear RestrictionsgeoNear$geoNeer约束

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 2dsphere index and/or multiple 2d index:这允许在具有多个2dsphere索引和/或多个2d索引的集合上使用$geoNear阶段:

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

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

Shard Key Restrictions分片键限制

You cannot use a 2dsphere index as a shard key when sharding a collection. 在对集合进行分片时,不能将2dsphere索引用作分片键However, you can create a geospatial index on a sharded collection by using a different field as the shard key.但是,您可以使用不同的字段作为分片键,在分片集合上创建地理空间索引。

2dsphere Indexed Field Restrictions索引字段限制

Fields with 2dsphere indexes must hold geometry data in the form of coordinate pairs or GeoJSON data. 具有2dsphere索引的字段必须以坐标对或GeoJSON数据的形式保存几何数据。If you attempt to insert a document with non-geometry data in a 2dsphere indexed field, or build a 2dsphere index on a collection where the indexed field has non-geometry data, the operation will fail.如果试图在2dsphere索引字段中插入包含非几何数据的文档,或在索引字段中包含非几何信息的集合上构建2dsphere索引,则操作将失败。

Limited Number of Index Keys索引键数量有限

To generate keys for a 2dsphere index, mongod maps GeoJSON shapes to an internal representation. 为了生成2dsphere索引的键,mongodGeoJSON形状映射到内部表示。The resulting internal representation may be a large array of values.产生的内部表示可以是一个大的值数组。

When mongod generates index keys on a field that holds an array, mongod generates an index key for each array element. mongod在保存数组的字段上生成索引键时,mongod为每个数组元素生成一个索引键。For compound indexes, mongod calculates the cartesian product of the sets of keys that are generated for each field. If both sets are large, then calculating the cartesian product could cause the operation to exceed memory limits.对于复合索引,mongod计算为每个字段生成的键集的笛卡尔积。如果两个集合都很大,那么计算笛卡尔积可能会导致操作超出内存限制。

indexMaxNumGeneratedKeysPerDocument limits the maximum number of keys generated for a single document to prevent out of memory errors. 限制为单个文档生成的最大密钥数,以防止内存不足错误。The default is 100000 index keys per document. 默认值为每个文档100000个索引键。It is possible to raise the limit, but if an operation requires more keys than the indexMaxNumGeneratedKeysPerDocument parameter specifies, the operation will fail.可以提高限制,但如果操作需要的键数超过indexMaxNumGeneratedKeysPerDocument参数指定的键数,则操作将失败。

Create a 2dsphere Index创建2dsphere索引

To create a 2dsphere index, use the db.collection.createIndex() method and specify the string literal "2dsphere" as the index type:要创建一个2dsphere索引,请使用db.collection.createIndex()方法并指定字符串文本"2dsphere"作为索引类型:

db.collection.createIndex( { <location field> : "2dsphere" } )

where the <location field> is a field whose value is either a GeoJSON object or a legacy coordinates pair.其中<location field>是值为GeoJSON对象传统坐标对的字段。

Unlike a compound 2d index which can reference one location field and one other field, a compound 2dsphere index can reference multiple location and non-location fields.与可以引用一个位置字段和另一个字段的复合2d索引不同,复合2dsphere索引可以引用多个位置字段或非位置字段。

For the following examples, consider a collection places with documents that store location data as GeoJSON Point in a field named loc:对于以下示例,请考虑一个集合places,其中包含将位置数据作为GeoJSON Point存储在名为loc的字段中的文档:

db.places.insertMany( [
   {
      loc : { type: "Point", coordinates: [ -73.97, 40.77 ] },
      name: "Central Park",
      category : "Parks"
   },
   {
      loc : { type: "Point", coordinates: [ -73.88, 40.78 ] },
      name: "La Guardia Airport",
      category : "Airport"
   }
] )

Create a 2dsphere Index创建2dsphere索引

The following operation creates a 2dsphere index on the location field loc:以下操作在位置字段loc上创建2dsphere索引:

db.places.createIndex( { loc : "2dsphere" } )

Create a Compound Index with 2dsphere Index Key使用2dsphere索引键创建复合索引

A compound index can include a 2dsphere index key in combination with non-geospatial index keys. 复合索引可以包括2dsphere索引键和非地理空间索引键。For example, the following operation creates a compound index where the first key loc is a 2dsphere index key, and the remaining keys category and names are non-geospatial index keys, specifically descending (-1) and ascending (1) keys respectively.例如,下面的操作创建了一个复合索引,其中第一个键loc是一个2dsphere索引键,其余的键categorynames是非地理空间索引键,具体来说分别是降序(-1)和升序(1)键。

db.places.createIndex( { loc : "2dsphere" , category : -1, name: 1 } )

Unlike the 2d index, a compound 2dsphere index does not require the location field to be the first field indexed. 2d索引不同,复合2dsphere索引不要求位置字段是第一个索引字段。For Example:例如:

db.places.createIndex( { category : 1 , loc : "2dsphere" } )
←  Wildcard Index RestrictionsQuery a 2dsphere Index →