Lesson 3: Managing Atlas Search Indexes via mongosh / Learn

Code Summary: Managing Atlas Search Indexes via mongosh

Create an Atlas Search Index Using the MongoDB Shell

To create an Atlas Search index using the MongoDB Shell, use the createSearchIndex method on the desired collection. Pass in a name and the index definition as arguments.

db.movies.createSearchIndex(
    "titleIndex",
     {
        "mappings": {
           "fields": {
              "title": {
                 "type": "string"
              }
           }
        }
     }
  )

View an Atlas Search Index Using the MongoDB Shell

To view an Atlas Search index using the MongoDB Shell, use the getSearchIndexes method on the desired collection. Pass in the name of the search index as an argument.

db.movies.getSearchIndexes("titleIndex")

List all Atlas Search Indexes Using the MongoDB Shell

To list all Atlas Search indexes using the MongoDB Shell, use the getSearchIndexes method on the desired collection with no arguments.

db.movies.getSearchIndexes()

Update an Atlas Search Index Using the MongoDB Shell

To update an Atlas Search index using the MongoDB Shell, use the updateSearchIndexes method on the desired collection. Pass in the name of the search index and the updated index definition as arguments.

db.movies.updateSearchIndex(
    "titleIndex",
    {
       "mappings": { "dynamic": true },
    }
 )

Delete an Atlas Search Index Using the MongoDB Shell

To delete an Atlas Search index using the MongoDB Shell, use the dropSearchIndex method on the desired collection. Pass in the name of the search index as an argument.

db.movies.dropSearchIndex("titleIndex")