🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to MongoDB Notes
Topic #22

MongoDB Indexing/Search


MongoDB Atlas comes with a full-text search engine that can be used to search for documents in a collection.

Atlas Search is powered by Apache Lucene.


Creating an Index

We'll use the Atlas dashboard to create an index on the "sample_mflix" database from the sample data that we loaded in the Intro to Aggregations section.

  1. From the Atlas dashboard, click on your Cluster name then the Search tab.
  2. Click on the Create Search Index button.
  3. Use the Visual Editor and click Next.
  4. Name your index, choose the Database and Collection you want to index and click Next. If you name your index "default" you will not have to specify the index name in the $search pipeline stage. Choose the sample_mflix database and the movies collection.
  5. Click Create Search Index and wait for the index to complete.

Running a Query

To use our search index, we will use the $search operator in our aggregation pipeline.

Example

db.movies.aggregate([
  {
    $search: {
      index: "default", // optional unless you named your index something other than "default"
      text: {
        query: "star wars",
        path: "title"
      },
    },
  },
  {
    $project: {
      title: 1,
      year: 1,
    }
  }
])

The first stage of this aggregation pipeline will return all documents in the movies collection that contain the word "star" or "wars" in the title field.

The second stage will project the title and year fields from each document.

Want to go beyond the notes?

Join CodingNow 2.0's MongoDB course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

MongoDB Indexing/Search – FAQs

Quick answers about learning MongoDB Indexing/Search in MongoDB.

This free note from CodingNow 2.0 explains MongoDB Indexing/Search in MongoDB — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every MongoDB topic on CodingNow 2.0, including MongoDB Indexing/Search, is 100% free with no signup required.
With focused practice, most students grasp MongoDB Indexing/Search in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now