翻译或纠错本页面

2dsphere 索引

2.4 新版功能.

Overview

A 2dsphere index supports queries that calculate geometries on an earth-like sphere. 2dsphere index supports all MongoDB geospatial queries: queries for inclusion, intersection and proximity. See the Geospatial Query Operators for the query operators that support geospatial queries.

The 2dsphere index supports data stored as GeoJSON objects and as legacy coordinate pairs (See also 在曲面上两点间线段和在平面上的两点间线段,可能包含的是不同的点集。在曲面上的线段是一条大地线(geodesic, 数学用语,曲面上两点间距离最短的线)。注意仔细检查点坐标,以避免共用边,或者重叠,或者其他交叉类型的错误。). For legacy coordinate pairs, the index converts the data to GeoJSON Point. For details on the supported GeoJSON objects, see GeoJSON Objects.

The default datum for an earth-like sphere is WGS84. Coordinate-axis order is longitude, latitude.

2dsphere (Version 2)

在 2.6 版更改.

MongoDB 2.6 introduces a version 2 of 2dsphere indexes. Version 2 is the default version of 2dsphere indexes created in MongoDB 2.6 and later series. To override the default version 2 and create a version 1 index, include the option { "2dsphereIndexVersion": 1 } when creating the index.

如下示例存储了GeoJSON 几何体集合 类型的坐标数据:

在 2.6 版更改.

一个 2dsphere 索引支持在类地球的球面上计算地理信息的查询。索引支持存储为 GeoJSON 对象和普通坐标的数据。索引支持普通坐标的方式是,将坐标转换为GeoJSON 单点 类型。在MongoDB2.4中,类地球球面的参考系默认使用的数据是 WGS84 。坐标顺序是 经度,纬度

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.

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.

其他GeoJSON对象

2dsphere (Version 2) includes support for additional GeoJSON object: MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection. For details on all supported GeoJSON objects, see GeoJSON Objects.

在更早版本的MongoDB中,只支持 2dsphere 的版本1. 版本1 2dsphere 索引默认 不是 稀疏的并且会拒绝保存那些在位置键上值为 null 的文档。

geoNear$geoNear 的限制

注意, geoNear 命令和 $geoNear 管道阶段要求一个集合中 最多 只能有一个 2dsphere index 或者 2d index ,但是 地理查询操作符 (例如 $near$geoWithin) 允许几何拥有多个地理索引。

The geospatial index restriction for the geoNear command and the $geoNear pipeline stage exists because neither the geoNear command nor the $geoNear pipeline stage syntax includes the location field. As such, index selection among multiple 2d indexes or 2dsphere indexes is ambiguous.

而对于 地理查询操作符 没有这样的限制是因为,这些操作符都要求指定一个位置键,消除了歧义。

分片键限制

You cannot use a 2dsphere index as a shard key when sharding a collection. However, you can create and maintain a geospatial index on a sharded collection by using a different field as the shard key.

在曲面上两点间线段和在平面上的两点间线段,可能包含的是不同的点集。在曲面上的线段是一条大地线(geodesic, 数学用语,曲面上两点间距离最短的线)。注意仔细检查点坐标,以避免共用边,或者重叠,或者其他交叉类型的错误。

Fields with 2dsphere indexes must hold geometry data in the form of coordinate pairs or GeoJSON data. 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.

Create a 2dsphere Index

To create a 2dsphere index, use the db.collection.createIndex() method, specifying the location field as the key and the string literal "2dsphere" as the index type:

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

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.

For the following examples, consider a collection places with documents that store location data as GeoJSON Point in a field named loc:

db.places.insert(
   {
      loc : { type: "Point", coordinates: [ -73.97, 40.77 ] },
      name: "Central Park",
      category : "Parks"
   }
)

db.places.insert(
   {
      loc : { type: "Point", coordinates: [ -73.88, 40.78 ] },
      name: "La Guardia Airport",
      category : "Airport"
   }
)

Create a 2dsphere Index

The following operation creates a 2dsphere index on the location field loc:

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

Create a Compound Index with 2dsphere Index Key

A compound index can include a 2dsphere index key in combination with non-geospatial index keys. 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.

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. For example:

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