Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Indexing & Search


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,
    }
  }
])
Try it Yourself »

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.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.