Navigation

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.

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.

../_images/indexes-view.png

For each index, Compass displays the following information:

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.

Create an Index

To create an index on a collection via Compass, the collection must contain documents.

1

Click the Create Index button.

From the Indexes tab, click Create Index to open 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.

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

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:

Option Description More Information
Build index in the background Ensure that the MongoDB deployment remains available during the index build operation. Background Construction
Create unique index Ensure that the indexed fields do not store duplicate values. Unique Indexes
Create a TTL index 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.

Example

The following partial filter expression only indexes documents where the timezone field exists:

{ "timezone": { "$exists": true } }
Partial Indexes
Use custom collation Create a custom collation for the index using the options provided in Compass. Collation Document
Wildcard projection (New in 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).

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.

Example

Consider a collection where documents contain a userMetadata object. The fields within the userMetadata object may vary between documents.

You can create a wildcard index on userMetadata to account for all potential fields within the object. In Compass, enter the following for the index name:

userMetadata.$**

Specify a type (ascending or descending) for your wildcard index, then click Create Index.

Compass shows the type of your new index as Wildcard.

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.
  • The Indexes tab is not available if you are connected to a Data Lake.