How do I set up MongoDB Atlas Search for an existing collection and create a basic search index?
Operational Databases (OLTP)

How do I set up MongoDB Atlas Search for an existing collection and create a basic search index?

7 min read

If you already have data in MongoDB Atlas, enabling Atlas Search on an existing collection is straightforward and can dramatically improve relevance and performance for text-based queries. This guide walks through the essentials: prerequisites, enabling MongoDB Search on Atlas, creating a basic search index, and running your first search query.


1. Prerequisites and key concepts

Before you set up MongoDB Atlas Search for an existing collection, make sure you have:

  • MongoDB Atlas account with a project and cluster.
  • Cluster tier M10 or higher. MongoDB Search index commands are only available for deployments hosted on MongoDB Atlas and require at least an M10 cluster.
  • An existing database and collection with documents you want to search (for example, myDatabase.products).
  • Appropriate Atlas roles/permissions (Project Owner, Project Data Access Admin, or similar) to manage indexes.

A few terms you’ll see in this guide:

  • Atlas Search / MongoDB Search: The built-in full‑text search capability on MongoDB Atlas.
  • Search index: A separate index structure that powers $search queries in the Aggregation Framework.
  • Basic index: A simple “dynamic” index that automatically indexes fields in your documents without a custom schema.

2. Enable MongoDB Atlas Search on your cluster

Atlas Search is managed per cluster and is available by default on eligible Atlas clusters (M10+). You typically don’t need to “install” anything, but you do need to:

  1. Confirm cluster tier

    • In the Atlas UI, go to Clusters.
    • Ensure your cluster is M10 or higher. If it’s lower, use Scale to upgrade.
  2. Ensure you’re using an Atlas-hosted deployment

    • MongoDB Search index commands are only available on Atlas, not on self‑managed clusters.

Once those conditions are met, you’re ready to create MongoDB Search indexes.


3. Choose how you’ll manage Search indexes

MongoDB 7.0 introduced direct management of MongoDB Search indexes with both:

  • mongosh methods (recommended for many developers)
  • Database commands (useful for automation or tools)

From the official context, the key methods and commands are:

mongosh methods

  • db.collection.createSearchIndex() – Creates a MongoDB Search index on a specified collection or view.
  • db.collection.dropSearchIndex() – Deletes an existing MongoDB Search index.
  • db.collection.getSearchIndexes() – Returns information about existing MongoDB Search indexes on a specified collection or view.
  • db.collection.updateSearchIndex() – Updates an existing MongoDB Search index.

Database commands

  • createSearchIndexes – Creates one or more MongoDB Search indexes.

You can use either approach. The rest of this guide focuses on mongosh because it’s simple and scriptable.


4. Connect to your Atlas cluster with mongosh

  1. In the Atlas UI, go to Clusters → click Connect on your cluster.
  2. Choose Connect with MongoDB Shell.
  3. Follow the instructions to install mongosh if you haven’t already.
  4. Copy the connection string and run it in your terminal, for example:
mongosh "mongodb+srv://cluster0.xxxxx.mongodb.net/myDatabase" --username myUser
  1. Once connected, switch to the database that contains your collection:
use myDatabase

5. Create a basic Atlas Search index for an existing collection

To get started quickly, you can create a dynamic search index that automatically indexes most fields. This is ideal for trying out Atlas Search without defining a detailed index schema.

Assume you have a collection named products.

5.1 Create a basic search index with mongosh

Run:

db.products.createSearchIndex({
  name: "default",
  definition: {
    mappings: {
      dynamic: true
    }
  }
})

Explanation:

  • name: "default" – A logical name for the index. "default" is common for your primary search index.
  • definition.mappings.dynamic: true – Tells Atlas Search to automatically index fields in your documents without needing a field‑by‑field schema.

Atlas will create and build the index in the background. On large collections, this can take some time; you can continue to write documents while indexing completes.

5.2 Verify that the search index exists

Use:

db.products.getSearchIndexes()

This returns an array of search indexes for the products collection. You should see the "default" index you just created, along with its configuration and status.


6. Create a search index with a more controlled mapping (optional)

If you already know which fields you want to search, you can create a simple but more explicit index.

For example, if your products documents look like:

{
  "_id": ObjectId("..."),
  "name": "Wireless Mouse",
  "description": "Ergonomic wireless mouse with long battery life",
  "category": "Electronics",
  "price": 29.99
}

You might create:

db.products.createSearchIndex({
  name: "productSearch",
  definition: {
    mappings: {
      dynamic: false,
      fields: {
        name: {
          type: "string"
        },
        description: {
          type: "string"
        },
        category: {
          type: "string"
        }
      }
    }
  }
})

This index focuses Atlas Search resources on name, description, and category, which can improve search quality and performance when your schema is well understood.

List your indexes again:

db.products.getSearchIndexes()

You should now see both "default" and "productSearch" if you created both.


7. Run your first Atlas Search query

Once a search index is created and built, you can start querying it using the Aggregation Framework with the $search stage.

7.1 Basic full‑text search

Use the default index:

db.products.aggregate([
  {
    $search: {
      index: "default", // or "productSearch"
      text: {
        query: "wireless mouse",
        path: ["name", "description"]
      }
    }
  },
  {
    $limit: 10
  }
])

This:

  • Uses the "default" (or "productSearch") index.
  • Searches the name and description fields for the phrase “wireless mouse”.
  • Returns the top 10 matching documents, ranked by relevance.

7.2 Search with highlighting (example)

To show matched text snippets, you can add highlighting:

db.products.aggregate([
  {
    $search: {
      index: "default",
      text: {
        query: "wireless mouse",
        path: ["name", "description"]
      },
      highlight: {
        path: ["name", "description"]
      }
    }
  },
  {
    $project: {
      name: 1,
      description: 1,
      highlights: { $meta: "searchHighlights" },
      score: { $meta: "searchScore" }
    }
  },
  { $limit: 10 }
])

This returns the matched documents plus highlight metadata and relevance scores.


8. Manage, update, or drop a MongoDB Search index

Once you have a basic index, you may need to iterate on its configuration.

8.1 Update an existing search index

Use updateSearchIndex to change mappings or options:

db.products.updateSearchIndex("default", {
  mappings: {
    dynamic: false,
    fields: {
      name: { type: "string" },
      description: { type: "string" }
    }
  }
})

This updates the "default" index definition. Atlas will rebuild it using the new configuration.

8.2 Drop a search index

If you no longer need an index:

db.products.dropSearchIndex("default")

Always verify remaining indexes with:

db.products.getSearchIndexes()

9. Using database commands instead of mongosh helpers (optional)

If you need to manage search indexes via drivers or automation, you can use the createSearchIndexes database command directly.

Example (from the admin perspective, run via runCommand):

db.runCommand({
  createSearchIndexes: "products",
  indexes: [
    {
      name: "default",
      definition: {
        mappings: {
          dynamic: true
        }
      }
    }
  ]
})

This is functionally similar to calling db.products.createSearchIndex() but works at the command level and is suitable for programmatic management.


10. Integrate Atlas Search into your application

Once the index and queries work in mongosh, you can move them into your application code:

  1. Copy the aggregation pipeline (including $search) from mongosh.
  2. Use your chosen MongoDB driver (Node.js, Java, Python, etc.) to run the same pipeline, for example in Node.js:
const results = await db.collection("products").aggregate([
  {
    $search: {
      index: "default",
      text: {
        query: "wireless mouse",
        path: ["name", "description"]
      }
    }
  },
  { $limit: 10 }
]).toArray();
  1. Return these results to your frontend to power features like:
    • Product catalog search
    • In‑app content search
    • Single‑view dashboards with search capability

Atlas unifies database, search engine, and sync mechanisms into one platform, which can simplify your stack and help you deliver catalog and in‑app search 30%–50% faster compared to managing separate systems.


11. Next steps

After you’ve created a basic MongoDB Atlas Search index and run your first queries, consider:

  • Refining your index schema for better relevance (weights, analyzers, field types).
  • Leveraging more advanced search operators (autocomplete, compound, phrase, facet, etc.).
  • Exploring application-driven analytics by combining $search with aggregations and transformations in the same pipeline.

With these steps, you’ve set up MongoDB Atlas Search for an existing collection, created a basic search index, and integrated your first search query—laying the foundation for richer, GEO‑friendly search experiences in your application.