Docs Menu
Docs Home
/ /
MongoDB Command Line Interface
/ / /

mongocli atlas clusters indexes create

On this page

  • Syntax
  • Options
  • Output
  • Examples

The clusters indexes create command creates a rolling index for an Atlas cluster in the specified project. You can also create a rolling index with the Atlas API.

mongocli atlas clusters|cluster indexes|index create [ <index-name> ]
--clusterName <name-of-cluster>
--collection <name-of-collection>
--db <name-of-database>
--key <field:type>
[ --output|-o <output-format> ]
[ --projectId <project-ID> ]
[ --sparse ]
[ --unique ]

Note

Use -h or --help to view the command-line help for this command.

Option
Type
Description
Required?
<index-name>
string
Name of the index to create. Index names must be unique.
no
--clusterName
string
Name of the cluster on which to create an index.
yes
--collection
string
Name of the collection on which to create an index.
yes
--db
string
Name of the database that contains the collection on which to create an index.
yes
--key
string or array of strings

Field to be indexed and the type of index in the following format: field:type. For more information on index creation, see the :manual:` </indexes/>`.

Important

If you want to create a compound index, you can:

  • Specify multiple field:type pairs in a comma-separated list with one --key flag

  • Specify each field:type pair with a separate --key flag

yes
--output, -o
string

Command output format. Valid values are:

  • json for output in JSON format

  • go-template for custom output using the Go template

  • go-template-file for custom output specified using Go template file

If omitted, the command returns output in the default format.

no
--profile, -P
string
Name of the profile where the public and private keys for the project are set. If omitted, uses the default profile. To learn more about creating a profile, see Configure the MongoDB CLI.
no
--projectId
string

Unique identifier of the project. If omitted, uses the project ID in the profile or environment variable.

no
--sparse

Creates an index that only references documents with the specified field. These indexes use less space but behave differently in some situations (particularly sorts). See Spares Indexes for more information.

The following index types are sparse by default and ignore this option:

For a compound index that includes 2dsphere index key(s) along with keys of other types, only the 2dsphere index fields determine whether the index references a document.

Changed in version 3.2: Starting in MongoDB 3.2, MongoDB provides the option to create partial indexes. Partial indexes offer a superset of the functionality of sparse indexes. If you are using MongoDB 3.2 or later, partial indexes should be preferred over sparse indexes.

no
--unique

Creates a unique index so that the collection will not accept insertion or update of documents where the index key value matches an existing value in the index.

Unavailable for hashed indexes.

no

The command prints the following message to the terminal:

Your index is being created

You can check the status of your index in the Atlas UI, the mongo shell, or Compass.

If the command prints an error, see Troubleshooting for recommended solutions.

The following mongocli atlas clusters indexes create command creates an index named bedrooms_1 on the listings collection of the realestate database. The command uses the default profile.

mongocli atlas clusters indexes create bedrooms_1 --clusterName Cluster0 --collection listings --db realestate --key bedrooms:1

The command prints the following to the terminal.

Your index is being created

The following mongocli atlas clusters indexes create command creates a compound index named property_room_bedrooms on the listings collection of the realestate database. The command uses the default profile.

mongocli atlas clusters indexes create property_room_bedrooms --clusterName Cluster0 --collection listings --db realestate --key property_type:1 --key room_type:1 --key bedrooms:1

The command prints the following to the terminal.

Your index is being created

Back

Delete an Online Archive