MongoDB Compass



MongoDB Compass automatically detects the URI connection string in the system clipboard and asks for permission to auto-fill the connection form with the appropriate information. Click Yes if you want to use the copied connection string. Fill in the Password field with the password for the user from the connection string. Click the Create Index button.¶ From the Indexes tab, click the Create Index button to bring up the.

Indexes are special data structures that improve query performance.Indexes store a portion of a collection's data in an easy-to-traverseform. 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 oftenin queries and for all operations thatsort by a field.

  • Queries on an indexed field can use the index to limit the number ofdocuments that must be scanned to find matching documents.
  • Sort operations on an indexed field can return documents pre-sortedby 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 expensivesince each insert must also update any indexes. For a detailed listof considerations for indexes, seeOperational 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 thecollection on the left hand pane and select the Indexes tab.

Installation

For each index, Compass displays the following information:

The name of the index and keys.
Regular, text, geospatial or hashed index.
How large the index is.
Number of times the index has been used since the time the indexwas created or the last server restart.
Any special properties (such as uniqueness, partial) of theindex.

Create an Index¶

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

Click the Create Index button.¶

From the Indexes tab, click theCreate Index button to bring up theCreate Index dialog.

Optional. Enter the index name.¶

In the dialog, enter the name of the index to create, or leave blankto have MongoDB create a default name for the index.

Add fields to index.¶

  1. Specify an index key.

    • To specify an existing document field as an index key, selectthe field from the dropdown list.
    • To specify a field which does not exist in any document as anindex 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 theindex type (ascending, descending, or2dsphere).

To learn how to specify a wildcard index, seeWildcard Indexes.

Optional. Specify the index options.¶

Compass supports the following index options:

Description
Build index in the background
Ensure that the MongoDB deployment remains available during
the index build operation.
Create unique index
Ensure that the indexed fields do not store duplicate values.
Create a TTL index
Delete documents automatically after a specified number ofseconds since the indexed field value.
Partial filter expression

Index only the documents which match the specified filterexpression.

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

Create a custom collation for the index using the optionsprovided in Compass.
Wildcard projection (New in MongoDB 4.2)

Support unknown or arbitrary fields which match the specifiedprojection in the index. To use a wildcard projection, setyour index field name to $**. This directsCompass to use all fields in the document (excluding_id).

Consider the following wildcard projection document:

If your index field name is $**, your index onlyincludes the values of the fields in that projection.

Click Create Index

Wildcard Indexes¶

You can create wildcard indexes tosupport queries against unknown or arbitrary fields.

MongoDBMongodb

Consider a collection where documents contain a userMetadataobject. The fields within the userMetadata object may varybetween documents.

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

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

MongoDB

Compass shows the type of your new index asWildcard.

Drop an Index¶

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 confirmationdialog appears.

Confirm the index to delete.¶

In the dialog, enter the name of the index to delete.

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 connectedto a Data Lake.

Starting in version 1.22, MongoDB Compass contains an embeddedMongoDB Shell. TheMongoDB Shell is a fully functional JavaScript environment forinteracting with MongoDB deployments. You can use the MongoDB Shell totest queries and operations directly with your database.

Open the Embedded MongoDB Shell¶

To open the embedded MongoDB Shell, click MongoSH Betaalong the bottom of MongoDB Compass.

Use the Embedded MongoDB Shell¶

MongoDB

When you initially open the MongoDB Shell, it connects to thetest database. To switch to a different database, runthe following command in the MongoDB Shell:

To run an operation in the embedded MongoDB Shell, type the operationinto the shell and press Enter.

Mongodb Compass

Multi-Line Operations in the Embedded MongoDB Shell¶

Install Mongodb Compass

To write an operation that spans multiple lines in the embeddedMongoDB Shell, press Shift + Enter to begin the nextline of code.

When you are finished writing your operation, press Enterto run it.

Learn More¶

Mongodb Compass Tutorial

The following links direct to theMongoDB Shell Documentation, which contains morecomplete reference for the MongoDB Shell, including syntax andbehaviors.

Mongodb

  • Learn how toPerform CRUD Operations in the MongoDB Shell.
  • Learn how toRun Aggregation Pipelines in the MongoDB Shell.
  • See a complete list ofAvailable MongoDB Shell Methods.