2dsphere
On this page本页内容
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. 2dsphere
索引支持所有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.对于传统坐标对,索引将数据转换为GeoJSON点。
2dsphere | |
---|---|
Version 3 | 2dsphere indexes. 2dsphere 索引的第3版。2dsphere indexes created in MongoDB 3.2 and later.2dsphere 索引的默认版本。 |
Version 2 | 2dsphere indexes. 2dsphere 索引的第2版。2dsphere indexes created in MongoDB 2.6 and 3.0 series. |
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 版本2和更高版本的2dsphere
indexes are always sparse and ignore the sparse option. 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 对于插入,MongoDB会插入文档,但不会添加到2dsphere
index.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 早期版本的MongoDB只支持2dsphere (Version 1)
indexes. 2dsphere (Version 1)
索引。默认情况下,2dsphere (Version 1)
indexes are not sparse by default and will reject documents with null
location fields.2dsphere (Version 1)
索引不是稀疏的,将拒绝包含null
位置字段的文档。
Version 2 and later 版本2及更高版本的2dsphere
indexes includes support for additional GeoJSON object: MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection. 2dsphere
索引包括对附加GeoJSON对象的支持:多点、多线、多多边形和GeometryCollection。For details on all supported GeoJSON objects, see GeoJSON Objects.有关所有受支持的GeoJSON对象的详细信息,请参阅GeoJSON对象。
geoNear
and $geoNear
RestrictionsgeoNear
和$geoNear
限制¶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 2dsphere
index and/or multiple 2d index:$geoNear
阶段用于具有多个2dsphere
索引和/或多个2d
索引的集合:
2dsphere
index and/or multiple 2d index, you must use the key
option to specify the indexed field path to use.2dsphere
索引和/或多个2d索引,则必须使用key
选项指定要使用的索引字段路径。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
索引,因为没有key
,多个2d
索引或2dsphere
索引之间的索引选区是不明确的。Note
If you do not specify the 如果不指定key
, and you have at most only one 2dsphere
index index and/or only one 2dsphere
index index, MongoDB looks first for a 2d
index to use. key
,并且最多只有一个2dsphere
索引和/或一个2dsphere
索引,MongoDB会首先查找要使用的二维索引。If a 如果2d
index does not exists, then MongoDB looks for a 2dsphere
index to use.2d
索引不存在,则MongoDB会查找要使用的2dsphere
索引。
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
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
索引,则操作将失败。
2dsphere
Index2dsphere
索引¶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"
指定为索引类型:
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 与可以引用一个位置字段和另一个字段的复合2d索引不同,复合2dsphere
index can reference multiple location and non-location fields.2dsphere
索引可以引用多个位置字段和非位置字段。
For the following examples, consider a collection 对于下面的示例,考虑一个集合位置,这些文档将存储位置数据作为GeoJSON点存储在一个名为places
with documents that store location data as GeoJSON Point in a field named loc
:loc
的字段中:
2dsphere
Index2dsphere
索引¶The following operation creates a 2dsphere index on the location field 以下操作在位置字段loc
:loc
上创建一个2dsphere
索引:
2dsphere
Index Key2dsphere
索引键创建复合索引¶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
索引键,其余的键类别和名称是非地理空间索引键,分别是降序键(-1
)和升序键(1
)。
Unlike the 2d index, a compound 与 2d索引不同,复合2dsphere
index does not require the location field to be the first field indexed. 2dsphere
索引不需要将位置字段作为第一个索引字段。For example:例如: