Database Manual / Indexes

Index Types索引类型

This page describes the types of indexes you can create in MongoDB. Different index types support different types of data and queries.此页面描述了可以在MongoDB中创建的索引类型。不同的索引类型支持不同类型的数据和查询。

Single Field Index单字段索引

Single field indexes collect and sort data from a single field in each document in a collection.单字段索引从集合中每个文档的单个字段集合数据并对其进行排序。

This image shows an index on a single field, score:此图显示了单个字段的索引score

Diagram of an index on the ``score`` field (ascending).

To learn more, see Single Field Indexes.要了解更多信息,请参阅单字段索引

Compound Index复合指数

Compound indexes collect and sort data from multiple field values from each document in a collection. You can use the compound index to query the first field or any prefix fields of the index. 复合索引从集合中每个文档的多个字段值中集合数据并对其进行排序。您可以使用复合索引查询索引的第一个字段或任何前缀字段。The order of fields in a compound index is very important. The B-tree created by a compound index stores the sorted data in the order that the index specifies the fields.复合索引中字段的顺序非常重要。由复合索引创建的B树按照索引指定字段的顺序存储排序后的数据。

For example, the following image shows a compound index where documents are first sorted by userid in ascending order (alphabetically). Then, the scores for each userid are sorted in descending order:例如,下图显示了一个复合索引,其中文档首先按userid升序(字母顺序)排序。然后,每个useridscores按降序排列:

Diagram of a compound index on the ``userid`` field (ascending) and the ``score`` field (descending). The index sorts first by the ``userid`` field and then by the ``score`` field.

To learn more, see Compound Indexes.要了解更多信息,请参阅复合索引

Multikey Index多键索引

Multikey indexes collect and sort data stored in arrays.多键索引集合并排序存储在数组中的数据。

You do not need to explicitly specify the multikey type. When you create an index on a field that contains an array value, MongoDB automatically sets the index to be a multikey index.您不需要显式指定多键类型。当您在包含数组值的字段上创建索引时,MongoDB会自动将索引设置为多键索引。

This image shows a multikey index on the addr.zip field:此图显示了addr.zip字段上的多键索引:

Diagram of a multikey index on the ``addr.zip`` field. The ``addr`` field contains an array of address documents. The address documents contain the ``zip`` field.

To learn more, see Multikey Indexes.要了解更多信息,请参阅多键索引

Geospatial Index地理空间索引

Geospatial indexes improve performance for queries on geospatial coordinate data. To learn more, see Geospatial Indexes.地理空间索引提高了地理空间坐标数据查询的性能。要了解更多信息,请参阅地理空间索引

MongoDB provides two types of geospatial indexes:MongoDB提供两种类型的地理空间索引:

  • 2d indexes索引 that use planar geometry to return results.使用平面几何来返回结果。
  • 2dsphere indexes索引 that use spherical geometry to return results.使用球面几何返回结果。

Text Index文本索引

Text indexes support text search queries on fields containing string content.文本索引支持对包含字符串内容的字段进行文本搜索查询。

To learn more, see Text Indexes on Self-Managed Deployments.要了解更多信息,请参阅自我管理部署上的文本索引

Note

Use MongoDB Search or Vector Search使用MongoDB搜索或矢量搜索

MongoDB also offers the following text search solutions:MongoDB还提供以下文本搜索解决方案:

  • MongoDB Search provides improved performance and functionality compared to on-premises text search.与本地文本搜索相比,提供了改进的性能和功能。
  • Vector Search provides vector search capabilities to perform semantic, hybrid, and generative search.提供向量搜索功能,以执行语义、混合和生成搜索。

Hashed Index散列索引

Hashed indexes support hashed sharding. Hashed indexes index the hash of the value of a field.哈希索引支持哈希分片。哈希索引对字段值的哈希值进行索引。

To learn more, see Hashed Indexes.要了解更多信息,请参阅哈希索引

Clustered Index聚集索引

New in version 5.3.在版本5.3中新增。

Clustered indexes specify the order in which clustered collections store data. Collections created with a clustered index are called clustered collections.聚集索引指定聚集集合存储数据的顺序。使用聚集索引创建的集合称为聚集集合。

To learn how to create a collection with a clustered index, see Clustered Collection Examples.要了解如何创建具有聚集索引的集合,请参阅聚集集合示例