Step 1: Create an Atlas Search Index
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.
Atlas UI
In Atlas, go to the Clusters page for your project.
If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it's not already displayed, select your desired project from the Projects menu in the navigation bar.
If it's not already displayed, click Clusters in the sidebar.
The Clusters page displays.
Go to the Atlas Search page for your cluster.
You can go the Atlas Search page from the sidebar, the Data Explorer, or your cluster details page.
In the sidebar, click Atlas Search under the Services heading.
From the Select data source dropdown, select your cluster and click Go to Atlas Search.
The Atlas Search page displays.
Click the Browse Collections button for your cluster.
Expand the database and select the collection.
Click the Search Indexes tab for the collection.
The Atlas Search page displays.
Click the cluster's name.
Click the Atlas Search tab.
The Atlas Search page displays.
Enter the Index Name, and set the Database and Collection.
In the Index Name field, enter
default
.If you name your index
default
, you don't need to specify anindex
parameter in the $search pipeline stage. If you give a custom name to your index, you must specify this name in theindex
parameter.In the Database and Collection section, find the
sample_mflix
database, and select themovies
collection.
Specify an index definition.
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.
Visual Editor
Click Next.
Review the
"default"
index definition for themovies
collection.
JSON Editor
Click Next.
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.Click Next.
Atlas Search API
Copy and paste the sample cURL request in your preferred text editor.
The following index definition dynamically indexes the fields of
supported types in the movies
collection.
1 PUBLIC_KEY=MY_PUBLIC_KEY # replace replace with your public key 2 PRIVATE_KEY=MY_PRIVATE_KEY # replace with your private key 3 GROUP_ID=YOUR_GROUP_ID # replace with your project ID 4 CLUSTER_NAME=YOUR_CLUSTER_NAME # replace with your cluster's name 5 6 curl --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 }'
Replace the variables in your sample cURL request.
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. |
Atlas CLI
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.
Create and save the JSON index definition in a file.
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 }
Copy and modify the sample Atlas CLI request.
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 |
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 |
Run the modified Atlas CLI request in your terminal to create the Atlas Search index.
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" }
MongoDB Compass
You can create an Atlas Search index on M10
and higher clusters running
MongoDB 7.0 or later from MongoDB Compass.
Connect to your Atlas cluster in MongoDB Compass.
Open Compass and connect to your Atlas cluster. For detailed instructions on connecting, see Connect via Compass.
Programatically
You can create an Atlas Search index programmatically by using
mongosh
or a supported MongoDB Driver
in your preferred language.
Prerequisites
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.
Procedure
Connect to your cluster in mongosh
.
Open mongosh
in a terminal window and
connect to your cluster. For detailed instructions on
connecting, see Connect via mongosh
.
Use the sample_mflix
database.
Run the following command in the mongosh
prompt:
use sample_mflix
Set up and initialize the .NET/C# project for the query.
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 Add the .NET/C# Driver to your project as a dependency.
dotnet add package MongoDB.Driver
Copy and paste the query into the Program.cs
file.
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);
Replace the <connection-string>
in the query and then save the file.
Ensure that your connection string includes your database user's credentials. To learn more, see Connect via Drivers.
Copy and paste the following code into the create-index.js
file.
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);
Replace the <connection-string>
in the query and then save the file.
Ensure that your connection string includes your database user's credentials. To learn more, see Connect via Drivers.
Next Steps
Now that you have created the index, proceed to Step 2: Run Atlas Search Queries.