Docs Home / Compass / Interact with Your Data

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.为了提高查询性能,请在查询中经常出现的字段以及按字段排序的所有操作上构建索引。

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.索引和键的名称。
TypeRegular, text, geospatial or hashed index.常规索引、文本索引、地理空间索引或哈希索引。
SizeHow large the index is.指数有多大。
UsageNumber of times the index has been used in a lookup since the time the index was created or the last server restart.自创建索引或上次服务器重新启动以来,在查找中使用索引的次数。
PropertiesAny special properties (such as uniqueness, partial) of the index.索引的任何特殊属性(如唯一性、局部性)。

Create an Index创建索引

1

Open the index creation dialog打开索引创建对话框

From the Indexes tab, click the Create Index button.“索引”选项卡中,单击“创建索引”按钮。

2

Add fields to the 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 that does not exist in any document as an index key, enter the field name in the input box.要将任何文档中都不存在的字段指定为索引键,请在输入框中输入字段名称。
    • To create a compound index, click the icon next to the index type dropdown.要创建复合索引,请单击索引类型下拉列表旁边的加号图标。
  2. Use the dropdown to the right of each field name to specify the index type. You can specify one of the following types:使用每个字段名称右侧的下拉列表指定索引类型。您可以指定以下类型之一:

    • Ascending升序
    • Descending降序
    • 2dsphere二维球面
    • Text文本

Tip

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

3

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

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

Option选项Description描述More Information详细信息
Create unique index索引Ensure that the indexed fields do not store duplicate values.确保索引字段不存储重复值。Unique Indexes唯一索引
Index name索引名称Specify a name for the index.指定索引的名称。Specify an Index Name指定索引名称
Create a TTL index创建TTL索引Delete documents automatically after a specified number of seconds since the indexed field value.在索引字段值后的指定秒数后自动删除文档。TTL Indexes索引
Partial filter expression部分筛选器表达式

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

For example:例如:

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

{ "timezone": { "$exists": true } }
Partial Indexes部分索引
Wildcard projection通配符投影

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)。

For 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.如果索引字段名为$**,则索引仅包含该投影中字段的值。

Create a Wildcard Index创建通配符索引
Use custom collation使用自定义排序规则Create a custom collation for the index by typing or pasting the collation document in the text box.通过在文本框中键入或粘贴排序规则文档,为索引创建自定义排序规则。Collation Document整理文件
4

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

Create an Atlas Search Index创建Atlas搜索索引

Atlas Search indexes let you query data in Atlas Search. For more information, see Create and Manage an Atlas Search Index.Atlas Search索引允许您在Atlas Search中查询数据。有关更多信息,请参阅创建和管理Atlas搜索索引

Create an Atlas Vector Search Index创建Atlas矢量搜索索引

Atlas Vector Search indexes enable you to index vector data and other data types, which facilitates semantic searches on the indexed fields. For more information, see Create an Atlas Vector Search Index.Atlas矢量搜索索引使您能够对矢量数据和其他数据类型进行索引,这有助于对索引字段进行语义搜索。有关更多信息,请参阅创建Atlas矢量搜索索引

Create a Wildcard Index创建通配符索引

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. The fields within the userMetadata object may vary between documents.考虑一个文档包含userMetadata对象的集合。userMetadata对象中的字段可能因文档而异。

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

userMetadata.$**

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

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

Hide or Unhide an Index隐藏或取消隐藏索引

You can hide an index from the query planner to evaluate the potential impact of dropping an index without actually dropping the index.您可以在查询计划器中隐藏索引,以评估在不实际删除索引的情况下删除索引的潜在影响。

1

Hover over the index.将鼠标悬停在索引上。

From the Indexes tab, hover over the index you want to hide.在“索引”选项卡中,将鼠标悬停在要隐藏的索引上。

2

Click the Hide Index button.单击“隐藏索引”按钮。

Click the closed-eye icon on the right that appears when you hover over your selected index.将鼠标悬停在所选索引上时,单击右侧出现的闭眼图标。

3

Click Confirm.单击“确认”。

In the dialog box, confirm the index you want to hide. After you confirm your selection, a Hidden badge appears under the Properties column.在对话框中,确认要隐藏的索引。确认选择后,“属性”列下会出现一个“隐藏”徽章。

To unhide your index, repeat steps 1-3. After you unhide your index, Compass removes the Hidden badge from the Properties column.要取消隐藏索引,请重复步骤1-3。取消隐藏索引后,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, hiding, and dropping indexes is not permitted in MongoDB Compass Readonly Edition.MongoDB Compass只读版不允许创建、隐藏和删除索引。
  • You can manage Atlas Search indexes in Compass if your deployment is local, has an Atlas cluster tier of M10 or larger, and runs MongoDB 7.0 or higher.如果部署是本地的,具有M10或更大的Atlas集群层,并且运行MongoDB 7.0或更高版本,则可以在Compass中管理Atlas搜索索引。

    Tip

    For clusters running an earlier version of MongoDB, you can manage your Atlas Search indexes using the Atlas UI, the Atlas CLI, or the Atlas Administration API.对于运行早期版本MongoDB的集群,您可以使用Atlas UI、Atlas CLIAtlas Administration API管理Atlas搜索索引。