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.要了解有关索引的更多信息,请参阅索引。
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.要访问集合的“索引”选项卡,请单击左侧窗格中的集合并选择“索引”选项卡。
For each index, Compass displays the following information:对于每个索引,Compass显示以下信息:
|
|
---|---|
|
|
|
|
|
|
|
|
|
|
Create an Index创建索引¶
To create an index on a collection via Compass, the collection must contain documents.要通过Compass在集合上创建索引,集合必须包含文档。
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为索引创建默认名称。
Add fields to index.将字段添加到索引。¶
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.要创建复合索引,请单击“添加其他字段”。
Use the dropdown to the right of each field name to specify the index type (使用每个字段名称右侧的下拉列表指定索引类型(ascending
,descending
, or 2dsphere).ascending
、descending
或2dsphere
)。
To learn how to specify a wildcard index, see Wildcard Indexes.要了解如何指定通配符索引,请参阅通配符索引。
Optional. Specify the index options.可选择的指定索引选项。¶
Compass supports the following index options:Compass支持以下索引选项:
|
|
|
---|---|---|
|
| |
|
| |
|
| |
|
Example
| |
|
| |
|
Example
|
Click Create Index.单击“创建索引”。¶
Wildcard Indexes通配符索引¶
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 (要在Compass中创建通配符索引,请在“选择字段名”输入中手动键入通配符索引字段(<field>.$**
) into the Select a field name input.<field>.$**
)。
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.ascending
或descending
),然后单击“创建索引”。
Compass shows the type of your new index as Wildcard.Compass将新索引的类型显示为“通配符”。
Drop an Index删除索引¶
Confirm the index to delete.确认要删除的索引。¶
In the dialog, enter the name of the index to delete.在对话框中,输入要删除的索引的名称。