MongoDB Text Indexes (original) (raw)

Last Updated : 5 May, 2026

MongoDB Text Indexes enable fast full-text search on string fields, allowing efficient querying of words and phrases across large text datasets, especially in unstructured or semi-structured data.

Create a Text Index in MongoDB

A text index is created using createIndex() on one or more string fields, enabling full-text search across multiple fields and allowing compound text indexes with non-text fields.

**Syntax:

db.collectionName.createIndex( { field: "text" } )

Example of Creating a Text Index

In this example, we will be working with

Screenshot-2026-02-19-142939

Create a text index on "title" field of "studentsposts" collection in order to search inside the collection.

**Query:

db.studentsposts.createIndex({title: "text"})

**Output:

Screenshot-2026-02-19-143056

Now, we will see how to search using Text Index:

**Query:

db.studentsposts.find({$text:{$search: "mongodb"}})

**Output:

Screenshot-2026-02-19-143207

Drop Text Index in MongoDB

Use db.collection.dropIndex() to remove a specific text index from a collection when it is no longer needed or needs to be recreated with different options.

**Syntax:

The syntax for MongoDB dropIndex method is:

db.collection.dropIndex("TextIndex")

Example of Drop Index in MongoDB

First, find the index of the field.

**Query:

db.studentsposts.getIndexes()

**Output:

Screenshot-2026-02-19-145232

Now, drop the text index using dropIndex() method.

**Query:

db.studentsposts.dropIndex("title_text")

Screenshot-2026-02-19-145327

Field Weights in Text Indexes

In MongoDB text indexes, field weights control the relevance score of documents by assigning higher importance to matches in specific fields during full-text search.

**Example:

db.studentsposts.createIndex(
{ title: "text", tags: "text" },
{
weights: { title: 10, tags: 5 },
name: "TextIndex"
}
)

Here, the weight of the title and tags are 10 and 5 respectively.

Screenshot-2026-02-19-150217

Wildcard Text Index in MongoDB

A wildcard text index ($**) indexes all string fields in a collection, enabling full-text search across dynamic or unknown fields in unstructured data.

Example of Wildcard Index in MongoDB

Create the text indexes using a wildcard specifier.

db.studentsposts.createIndex( { "$**": "text" } )

**Output:

Screenshot-2026-02-19-150638

Check all the indexes present in the studentsposts collection.

Screenshot-2026-02-19-150753

Limitations of MongoDB Text Indexes

While text indexes are powerful, there are some important limitations to be aware of:

Important Points About MongoDB Text Indexes

MongoDB Text Indexes provide efficient full-text search capabilities with support for weighted fields, multi-field indexing, and easy index management.