Docs Menu

Create an Atlas Search Index using the MongoDB CLI

On this page

  • Prerequisites
  • Procedure
  • Next Steps
Important
Serverless Instances are in Preview

Serverless instances are in preview and do not support this feature at this time. To learn more, see Serverless Instance Limitations.

This part of the tutorial guides you through creating an Atlas Search index in the MongoDB CLI for Atlas Search.

Tip

You can also create Atlas Search indexes using the Atlas UI and Atlas Search API.

This tutorial uses the sample_mflix database and the movies collection from the Sample Mflix Dataset.

To learn more about MongoDB CLI Atlas clusters search indexes, see MongoDB CLI Atlas clusters search.

To create an Atlas Search index using the MongoDB CLI, you must:

1

Copy the appropriate index file example below, then paste the JSON index example into a new file and save the file:

Dynamic Mapping

The following index definition dynamically indexes the fields of supported types in the movies collection.

1{
2 "name": "INDEX_NAME",
3 "clusterName": "CLUSTER_NAME",
4 "collectionName": "movies",
5 "database": "sample_mflix",
6 "mappings": {
7 "dynamic": true
8 }
9}

Static Mapping

The movies collection is large. To save space, we're indexing only the title, genres, and plot fields. The following index definition specifies the standard analyzer as the default analyzer for all three indexed fields.

Important

When you create an Atlas Search index using MongoDB CLI, you can't specify alternate analyzers for specific fields. Instead:

1{
2 "name": "INDEX_NAME",
3 "collectionName": "movies",
4 "database": "sample_mflix",
5 "mappings": {
6 "dynamic": false,
7 "fields": {
8 "genres": {
9 "analyzer": "lucene.standard",
10 "type": "string"
11 },
12 "plot": {
13 "analyzer": "lucene.standard",
14 "type": "string"
15 },
16 "title": {
17 "analyzer": "lucene.standard",
18 "type": "string"
19 }
20 }
21 }
22}
2

Paste the following sample MongoDB CLI request into your terminal and replace the variables.

mongocli atlas clusters search index create \
--clusterName CLUSTER_NAME \
--file FILE_PATH \
--projectId PROJECT_ID
--profile PROFILE_NAME

The sample MongoDB CLI requests use these variables. Replace these variables with your desired values before running the command to create an Atlas Search index.

Name
Type
Description
PROJECT_ID
string
Unique 24-hexadecimal character string that identifies the project that contains the cluster. The cluster contains the collection for which you want to create an Atlas Search index.
CLUSTER_NAME
string
Unique 24-hexadecimal character string that identifies the cluster. The cluster contains the collection for which you want to create an Atlas Search index.
FILE_PATH
string
Path to the JSON index file that you created and saved in the previous steps, including the .json file extension.
PROFILE_NAME
string
Optional. Name of the profile that sets the public and private keys for the project. See Configure the MongoDB CLI for more information. If you remove the --profile flag, MongoDB CLI uses the default profile.
3

Run the request. You receive this response when Atlas Search begins creating the index:

{
"collectionName": "movies",
"database": "sample_mflix",
"indexID": <index-id>,
"mappings": {
"dynamic": true
},
"name": <index-name>,
"status": "IN_PROGRESS"
}

Now that you have created the index, proceed to Run Atlas Search Queries.

Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.