Docs HomeMongoDB Compass

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.有关索引注意事项的详细列表,请参阅索引的操作注意事项

Indexes Tab“索引”选项卡

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 in a lookup since the time the index was created or the last server restart.自创建索引或上次服务器重新启动以来,在查找中使用索引的次数。
Properties属性Any special properties (such as uniqueness, partial) of the index.索引的任何特殊属性(如唯一性、部分性)。

Create an Index创建索引

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

1

Click the Create Index button.单击“创建索引”按钮。

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

2

Optional. 可选。Enter the index name.输入索引名称。

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

Add fields to index.将字段添加到索引中。

  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

Optional. 可选。Specify the index options.指定索引选项。

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

Option选项Description描述More Information更多信息
Build index in the background在后台生成索引 Ensure that the MongoDB deployment remains available during the index build operation.确保MongoDB部署在索引构建操作期间仍然可用。Background Construction背景建设
Create unique index创建唯一索引Ensure that the indexed fields do not store duplicate values.确保索引字段不存储重复的值。Unique Indexes唯一索引
Create a TTL index创建TTL索引Delete documents automatically after a specified number of seconds since the indexed field value.自索引字段值之后的指定秒数后自动删除文档。TTL IndexesTTL索引
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 } }
Partial Indexes部分索引
Use custom collation使用自定义排序规则Create a custom collation for the index using the options provided in Compass.使用Compass中提供的选项为索引创建自定义排序规则。Collation Document校对文档
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.如果索引字段名称为$**,则索引仅包括该投影中字段的值。
Wildcard Indexes通配符索引
5

Click Create Index.单击“创建索引”。

Wildcard Indexes通配符索引

New in version 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将新索引的类型显示为“通配符”。

Drop an Index删除索引

1

Click the trash can icon for the index to drop.单击要删除的索引的垃圾桶图标。

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

2

Confirm the index to delete.确认要删除的索引。

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

3

Click Drop to drop the index.单击“删除”以删除索引。

Limitations局限性

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