Geospatial Indexes地理空间索引
On this page本页内容
Geospatial indexes support queries on data stored as GeoJSON objects or legacy coordinate pairs. 地理空间索引支持对存储为GeoJSON对象或遗留坐标对的数据进行查询。You can use geospatial indexes to improve performance for queries on geospatial data or to run certain geospatial queries.可以使用地理空间索引来提高地理空间数据查询的性能,也可以运行某些地理空间查询。
MongoDB provides two types of geospatial indexes:MongoDB提供两种类型的地理空间索引:
2dsphere
Indexes索引, which support queries that interpret geometry on a sphere.,支持解释球体上的几何图形的查询。2d
Indexes索引, which support queries that interpret geometry on a flat surface.,支持解释平面上的几何图形的查询。
To learn more about geospatial data and query operations, see Geospatial Queries.要了解有关地理空间数据和查询操作的详细信息,请参阅地理空间查询。
Use Cases用例
If your application frequently queries a field that contains geospatial data, you can create a geospatial index to improve performance for those queries.如果应用程序经常查询包含地理空间数据的字段,则可以创建地理空间索引以提高这些查询的性能。
Certain query operations require a geospatial index. 某些查询操作需要地理空间索引。If you want to query with the 如果要使用$near
or $nearSphere
operators or the $geoNear
aggregation stage, you must create a geospatial index. $near
或$nearSphere
运算符或$geoNear
聚合阶段进行查询,则必须创建地理空间索引。For details, see Geospatial Query Operators and Geospatial Aggregation Stage.有关详细信息,请参阅地理空间查询运算符和地理空间聚合阶段。
For example, consider a 例如,考虑一个包含subway
collection with documents containing a location
field, which specifies the coordinates of subway stations in a city. location
字段的文档的subway
集合,该字段指定城市中地铁站的坐标。You often run queries with the 您经常使用$geoWithin
operator to return a list of stations within a specific area. $geoWithin
运算符运行查询,以返回特定区域内的电台列表。To improve performance for this query, you can create a geospatial index on the 若要提高此查询的性能,可以在location
field. location
字段上创建地理空间索引。After creating the index, you can query using the 创建索引后,您可以使用$near
operator to return a list of nearby stations, sorted from nearest to farthest.$near
运算符进行查询,以返回从最近到最远排序的附近站点列表。
Get Started起步
To create a geospatial index and run geospatial queries, see:要创建地理空间索引并运行地理空间查询,请参阅:
Details详细信息
This section describes details about geospatial indexes.本节介绍有关地理空间索引的详细信息。
Sharded Collections分片集合
You can't use a geospatial index as a shard key when sharding a collection. 对集合进行分片时,不能将地理空间索引用作分片键。However, you can create a geospatial index on a sharded collection using a different field as the shard key.但是,可以使用不同的字段作为分片键在分片集合上创建地理空间索引。
You can use geospatial query operators and aggregation stages to query for geospatial data on sharded collections.可以使用地理空间查询运算符和聚合阶段来查询分片集合上的地理空间数据。
Prior to MongoDB 4.0, 在MongoDB 4.0之前,分片集合不支持$near
and $nearSphere
queries are not supported for sharded collections. $near
和$nearSphere
查询。Instead, you can use the 相反,您可以使用$geoNear
aggregation stage or the geoNear
command.$geoNear
聚合阶段或geoNear命令。
Covered Queries涵盖的查询
Geospatial indexes can't cover a query.地理空间索引无法覆盖查询。
Spherical Queries球形查询
Using a 使用2d
index for queries on spherical data can return incorrect results or an error. 2d
索引查询球形数据可能会返回不正确的结果或错误。For example, 例如,2d
indexes don't support spherical queries that wrap around the poles.2d
索引不支持环绕极点的球形查询。
However, you can use the 但是,可以将2dsphere
index for both spherical queries and two-dimensional queries. 2dsphere
索引用于球形查询和二维查询。For two-dimensional queries, the 对于二维查询,2dsphere
index converts data stored as legacy coordinate pairs to the GeoJSON Point type.2dsphere
索引将存储为遗留坐标对的数据转换为GeoJSON Point类型。
Learn More了解更多信息
For sample geospatial query operations, see Geospatial Query Examples.有关地理空间查询操作示例,请参阅地理空间查询示例。