Manage Indexes管理索引

Indexes are special data structures that improve query performance. 索引是提高查询性能的特殊数据结构。Indexes store a portion of a collection's data in an easy-to-traverse form. 索引以易于遍历的形式存储集合数据的一部分。The index stores the value of a specific field or set of fields, ordered by the value of the field.索引存储特定字段或字段集的值,按字段值排序。

To improve query performance, build indexes on fields that appear often in queries and for all operations that sort by a field.要提高查询性能,请对查询中经常出现的字段以及按字段排序的所有操作建立索引。

  • Queries on an indexed field can use the index to limit the number of documents that must be scanned to find matching documents.对索引字段的查询可以使用索引来限制必须扫描以查找匹配文档的文档数。
  • Sort operations on an indexed field can return documents pre-sorted by the index.索引字段上的排序操作可以返回按索引预先排序的文档。

To learn more about indexes, see Indexes.要了解有关索引的更多信息,请参阅索引

Note
Considerations考虑事项

Indexes have some negative performance impact on write operations. 索引对写操作有一些负面的性能影响。For collections with high write-to-read ratio, indexes are expensive since each insert must also update any indexes. 对于具有高读写比的集合,索引是昂贵的,因为每次插入都必须更新任何索引。For a detailed list of considerations for indexes, see Operational Considerations for Indexes.有关索引注意事项的详细列表,请参阅索引的操作注意事项

The Indexes tab lists the existing indexes for a collection.“索引”选项卡列出集合的现有索引。

To access the Indexes tab for a collection, click on the collection on the left hand pane and select the Indexes tab.要访问集合的“索引”选项卡,请单击左侧窗格中的集合并选择“索引”选项卡。

Indexes view

For each index, Compass displays the following information:对于每个索引,Compass显示以下信息:

Name and Definition名称和定义
The name of the index and keys.索引和键的名称。
Type类型
Regular, text, geospatial or hashed index.常规索引、文本索引、地理空间索引或哈希索引。
Size大小
How large the index is.索引有多大。
Usage用法
Number of times the index has been used since the time the index was created or the last server restart.自创建索引或上次服务器重新启动以来使用索引的次数。
Properties性质
Any special properties (such as uniqueness, partial) of the index.索引的任何特殊属性(如唯一性、部分性)。

To create an index on a collection via Compass, the collection must contain documents.要通过Compass在集合上创建索引,集合必须包含文档。

1

From the Indexes tab, click the Create Index button to bring up the Create Index dialog.在“索引”选项卡中,单击“创建索引”按钮以打开“创建索引”对话框。

2

In the dialog, enter the name of the index to create, or leave blank to have MongoDB create a default name for the index.在对话框中,输入要创建的索引的名称,或留空让MongoDB为索引创建默认名称。

3
  1. Specify an index key.指定索引键。

    • To specify an existing document field as an index key, select the field from the dropdown list.若要将现有文档字段指定为索引键,请从下拉列表中选择该字段。
    • To specify a field which does not exist in any document as an index key, enter the field name in the input box.要将任何文档中不存在的字段指定为索引键,请在输入框中输入字段名称。
    • To create a compound index, click Add Another Field.要创建复合索引,请单击“添加其他字段”。
  2. Use the dropdown to the right of each field name to specify the index type (ascending, descending, or 2dsphere).使用每个字段名称右侧的下拉列表指定索引类型(ascendingdescending2dsphere)。
Tip
See also:参阅:

To learn how to specify a wildcard index, see Wildcard Indexes.要了解如何指定通配符索引,请参阅通配符索引

4

Compass supports the following index options:Compass支持以下索引选项:

Option选项
Description描述
More Information更多信息
Build index in the background在后台建立索引
Ensure that the MongoDB deployment remains available during确保MongoDB部署在运行期间保持可用
the index build operation.索引生成操作。
Create unique index创建唯一索引
Ensure that the indexed fields do not store duplicate values.确保索引字段不存储重复的值。
Create a TTL index创建一个TTL索引
Delete documents automatically after a specified number of seconds since the indexed field value.自索引字段值起指定秒数后自动删除文档。
Partial filter expression偏滤表达式

Index only the documents which match the specified filter expression.仅索引与指定筛选器表达式匹配的文档。

Example

The following partial filter expression only indexes documents where the timezone field exists:以下部分筛选器表达式仅对存在timezone字段的文档进行索引:

{ "timezone": { "$exists": true } }
Use custom collation使用自定义排序规则
Create a custom collation for the index using the options provided in Compass.使用Compass中提供的选项为索引创建自定义排序规则。
Wildcard projection (New in MongoDB 4.2)通配符投影(MongoDB 4.2中新增)

Support unknown or arbitrary fields which match the specified projection in the index. 支持与索引中指定投影匹配的未知或任意字段。To use a wildcard projection, set your index field name to $**. 要使用通配符投影,请将索引字段名设置为$**This directs Compass to use all fields in the document (excluding _id).这将指示Compass使用文档中的所有字段(不包括_id)。

Example

Consider the following wildcard projection document:考虑以下通配符投影文档:

{
  "product_attributes.elements" : 1,
  "product_attributes.resistance" : 1
}

If your index field name is $**, your index only includes the values of the fields in that projection.如果索引字段名为$**,则索引仅包括该投影中字段的值。

5

New in version v4.2.v4.2版中的新增功能

You can create wildcard indexes to support queries against unknown or arbitrary fields. 您可以创建通配符索引以支持对未知或任意字段的查询。To create a wildcard index in Compass, manually type the wildcard index field (<field>.$**) into the Select a field name input.要在Compass中创建通配符索引,请在“选择字段名”输入中手动键入通配符索引字段(<field>.$**)。

Example

Consider a collection where documents contain a userMetadata object. 考虑文档包含userMetadata对象的集合。The fields within the userMetadata object may vary between documents.userMetadata对象中的字段可能因文档而异。

You can create a wildcard index on userMetadata to account for all potential fields within the object. 您可以在userMetadata上创建通配符索引,以说明对象中的所有潜在字段。Type the following into the Select a field name input:在“选择字段名”输入中键入以下内容:

userMetadata.$**

Specify a type (ascending or descending) for your wildcard index, then click Create Index.为通配符索引指定类型(ascendingdescending),然后单击“创建索引”。

Compass shows the type of your new index as Wildcard.Compass将新索引的类型显示为“通配符”。

1

From the Indexes tab, to delete an index, click on the trash can icon for that index. 在“索引”选项卡中,要删除索引,请单击该索引的垃圾箱图标。A confirmation dialog appears.此时会出现一个确认对话框。

2

In the dialog, enter the name of the index to delete.在对话框中,输入要删除的索引的名称。

3
  • Creating and dropping indexes is not permitted in MongoDB Compass Readonly Edition.MongoDB Compass只读版不允许创建和删除索引。
  • The Indexes tab is not available if you are connected to a Data Lake.如果连接到数据湖,“索引”选项卡不可用。