Docs Menu
Docs Home
/
MongoDB Atlas
/ /

Step 1: Create an Atlas Search Index

On this page

  • Atlas UI
  • Atlas Search API
  • Atlas CLI
  • MongoDB Compass
  • Programatically
  • Next Steps

Important

Feature unavailable in Serverless Instances

Serverless instances don't support this feature at this time. To learn more, see Serverless Instance Limitations.

An Atlas Search index is a data structure that categorizes data in an easily searchable format. It is a mapping between terms and the documents that contain those terms. Atlas Search indexes enable faster retrieval of documents using certain identifiers. You must configure an Atlas Search index to query data in your Atlas cluster using Atlas Search.

You can create an Atlas Search index on a single field or on multiple fields. We recommend that you index the fields that you regularly use to sort or filter your data in order to quickly retrieve the documents that contain the relevant data at query-time.

You can create an Atlas Search index using the Atlas UI, Atlas Search API, Atlas CLI, MongoDB Compass, or a supported MongoDB Driver in your preferred language.

This tutorial uses the sample_mflix.movies collection from the sample datasets.

1
  1. If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your desired project from the Projects menu in the navigation bar.

  3. If it's not already displayed, click Clusters in the sidebar.

    The Clusters page displays.

2

You can go the Atlas Search page from the sidebar, the Data Explorer, or your cluster details page.

  1. In the sidebar, click Atlas Search under the Services heading.

  2. From the Select data source dropdown, select your cluster and click Go to Atlas Search.

    The Atlas Search page displays.

  1. Click the Browse Collections button for your cluster.

  2. Expand the database and select the collection.

  3. Click the Search Indexes tab for the collection.

    The Atlas Search page displays.

  1. Click the cluster's name.

  2. Click the Atlas Search tab.

    The Atlas Search page displays.

3
4
  • For a guided experience, select the Atlas Search Visual Editor.

  • To edit the raw index definition, select the Atlas Search JSON Editor.

5
  1. In the Index Name field, enter default.

    If you name your index default, you don't need to specify an index parameter in the $search pipeline stage. If you give a custom name to your index, you must specify this name in the index parameter.

  2. In the Database and Collection section, find the sample_mflix database, and select the movies collection.

6

You can create an Atlas Search index that uses dynamic mappings or static mappings. To learn more about dynamic and static mappings, see Static and Dynamic Mappings.

The following index definition dynamically indexes the fields of supported types in the movies collection. You can use the Atlas Search Visual Editor or the Atlas Search JSON Editor in the Atlas user interface to create the index.

  1. Click Next.

  2. Review the "default" index definition for the movies collection.

  1. Click Next.

  2. Review the index definition.

    Your index definition should look similar to the following:

    {
    "mappings": {
    "dynamic": true
    }
    }

    The above index definition dynamically indexes the fields of supported types in each document in the movies collection.

  3. Click Next.

7
8
9

A modal window appears to let you know your index is building. Click the Close button.

10

The index should take about one minute to build. While it is building, the Status column reads Build in Progress. When it is finished building, the Status column reads Active.

1

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

1PUBLIC_KEY=MY_PUBLIC_KEY # replace replace with your public key
2PRIVATE_KEY=MY_PRIVATE_KEY # replace with your private key
3GROUP_ID=YOUR_GROUP_ID # replace with your project ID
4CLUSTER_NAME=YOUR_CLUSTER_NAME # replace with your cluster's name
5
6curl --user "$PUBLIC_KEY:$PRIVATE_KEY" --digest \
7 --header "Content-Type: application/json" \
8 --include \
9 --request POST "https://mongodb.prakticum-team.ru/proxy/cloud.mongodb.com/api/atlas/v2/groups/$GROUP_ID/clusters/$CLUSTER_NAME/search/indexes?pretty=true" \
10 --data '{
11 "collectionName": "movies",
12 "database": "sample_mflix",
13 "definition":{
14 "mappings": {
15 "dynamic": true
16 }
17 },
18 "name": "default"
19 }'
2

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

Name
Type
Description

PUBLIC_KEY

string

Your public API Key for your API credentials.

PRIVATE_KEY

string

Your private API Key for your API credentials.

GROUP_ID

string

Unique 24-hexadecimal character string that identifies the project that contains the cluster that contains the collection for which you want to create an Atlas Search index.

CLUSTER_NAME

string

Human-readable label that identifies the cluster that contains the collection for which you want to create an Atlas Search index.

Use the API to get the CLUSTER_NAME. For each cluster, Atlas returns the CLUSTER_NAME in the name field.

3
1{
2 "collectionName" : "movies",
3 "database" : "sample_mflix",
4 "indexID" : "60bfd25f59fc81594354eed3",
5 "mappings" : {
6 "dynamic" : true
7 },
8 "name" : "default",
9 "status" : "IN_PROGRESS"
10}

Note

You can also use Atlas Search with local Atlas deployments that you create with the Atlas CLI. To learn more, see Create a Local Atlas Deployment.

1

Copy the following sample index definition, then paste the JSON index example into a new file, and save the file:

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}
2

Paste the following sample Atlas CLI request into your favorite text editor and replace the variables.

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

The sample Atlas 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 Save Connection Settings for more information. If you remove the --profile flag, the Atlas 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"
}

You can create an Atlas Search index on M10 and higher clusters running MongoDB 7.0 or later from MongoDB Compass.

1

Open Compass and connect to your Atlas cluster. For detailed instructions on connecting, see Connect via Compass.

2

On the Database screen, click the sample_mflix database, then click the movies collection.

3
  1. Click the Indexes tab, then select Search Indexes.

  2. Click Create Index to open the index creation dialog box.

  3. Specify a name for the index and then the search index definition.

    Index Name

    default

    Index Definition

    {
    mappings: { dynamic: true }
    }
  4. Click Create Search Index.

You can create an Atlas Search index programmatically by using mongosh or a supported MongoDB Driver in your preferred language.

To create and manage Atlas Search indexes programmatically on any cluster tier, you can use any of the following MongoDB Drivers:

MongoDB Driver
Version

1.25.0 or higher

3.9.0 or higher

2.21.0 or higher

1.13.0 or higher

4.11.0 or higher

9.0 or higher

3.3.0 or higher

5.2.0 or higher

5.6.0 or higher

1.17.0 or higher

4.5 or higher

2.19.2 or higher

2.8.0 or higher

5.2.0 or higher


➤ Use the Select your language drop-down menu to set the language of the example in this section.


1

Open mongosh in a terminal window and connect to your cluster. For detailed instructions on connecting, see Connect via mongosh.

2

Run the following command in the mongosh prompt:

use sample_mflix
3
db.movies.createSearchIndex(
"default",
{ mappings: { dynamic: true } }
)
default
1
  1. Create a new directory called create-index-tutorial and initialize your project with the dotnet new command.

    mkdir create-index-tutorial
    cd create-index-tutorial
    dotnet new console
  2. Add the .NET/C# Driver to your project as a dependency.

    dotnet add package MongoDB.Driver
2
Program.cs
using MongoDB.Bson;
using MongoDB.Driver;
// connect to your Atlas deployment
var uri = "<connection-string>";
var client = new MongoClient(uri);
var db = client.GetDatabase("sample_mflix");
var collection = db.GetCollection<BsonDocument>("movies");
// define your Atlas Search index
var index = new CreateSearchIndexModel(
"default", new BsonDocument
{
{ "mappings", new BsonDocument
{
{ "dynamic", true }
}
}
});
var result = collection.SearchIndexes.CreateOne(index);
Console.WriteLine(result);
3

Ensure that your connection string includes your database user's credentials. To learn more, see Connect via Drivers.

4
dotnet run csharp-create-index.csproj
default
1
2
create-index.js
const { MongoClient } = require("mongodb");
// connect to your Atlas deployment
const uri =
"<connection-string>";
const client = new MongoClient(uri);
async function run() {
try {
// set namespace
const database = client.db("sample_mflix");
const collection = database.collection("movies");
// define your Atlas Search index
const index = {
name: "default",
definition: {
/* search index definition fields */
"mappings": {
"dynamic": true
}
}
}
// run the helper method
const result = await collection.createSearchIndex(index);
console.log(result);
} finally {
await client.close();
}
}
run().catch(console.dir);
3

Ensure that your connection string includes your database user's credentials. To learn more, see Connect via Drivers.

4

To create the index, run the following command:

node create-index.js
default

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