MongoDB Text Indexes
MongoDB Text Indexes are an essential feature for efficiently performing full-text search operations on text fields in a collection. This powerful tool enables us to search for words, phrases, and variations within text-based data, significantly improving performance for large datasets.
In this article, we will explore the MongoDB text index functionality, how to create and manage them, their limitations, and best practices for efficient use.
What are MongoDB Text Indexes?
MongoDB text indexes are designed to optimize queries on text-based fields, enabling full-text search. When a field in a collection is indexed as a text index, MongoDB creates an index on the string values, allowing for fast searches through large amounts of textual data.
This feature is especially useful for querying unstructured or semi-structured data where you need to search for words, phrases, or partial matches within string fields.
Key Features of MongoDB Text Indexes:
- Supports text search queries for efficient searching of text fields.
- Enables searching for exact words, partial matches, phrases, and text variations.
- Optimizes queries on fields containing strings, supporting various text search operators like
$text
,$search
, and$language
.
How to Create a Text Index in MongoDB
Creating a text index in MongoDB is simple and can be done using the createIndex()
method. We can create a text index on one or more fields, making it possible to search across multiple fields in a single query. We can create a compound text index to index multiple fields simultaneously or combine text fields with other types of indexes.
Syntax
db.collectionName.createIndex( { field: "text" } )
Example of Creating a Text Index
Let's look at some examples of Text Indexes in MongoDB to understand the concept better. In this example, we will be working with
- Database: gfg
- Collection: studentsposts
- Documents: Two documents

Now, Let us create a text index on "title" field of "studentsposts" collection in order to search inside the collection.
Query:
db.studentsposts.createIndex({title: "text"})
Output:

Now we will see how to search using Text Index:
Query:
db.studentsposts.find({$text:{$search: "mongodb"}}).pretty()
Output:

Output is self-explanatory above as we have created index on "title" field, and we have tried to search the text "mongodb". It is present in both the documents in the "title" field. Hence, the result is 2 documents here.
Drop Index in MongoDB
Sometimes there may be necessities to delete the text indexes too as it was created wrongly or need to be modified in a different manner or totally want to delete that. So, using db.collection.dropIndex() method we can delete text index. This method deletes the specified index from the given collection.
Syntax
The syntax for MongoDB dropIndex method is:
db.collection.dropIndex("TextIndex")
MongoDB Drop Index in Example
First, we find the index of the field.
Query:
db.studentsposts.getIndexes()
Output:

Now we drop the text index using dropIndex() method.
Query:
db.studentsposts.dropIndex("title_text")

Specify weights
For a text index, the weight of an indexed field is the significance of the field. In MongoDB, for each index field in the document, MongoDB sums the results by multiplying the number of matches by weight. Now using this sum, MongoDB calculates the score for the document. The default weight of the index field is 1 and you can adjust the weight of the index using createIndex() method.
Example:
db.studentsposts.createIndex({title:"text", tags:"text"},
{weights:{title:10, tags:5},
name:"TextIndex"})
Here, the weight of the title and tags field is 10 and 5.

Wildcard Text Index in MongoDB
Using wildcard specifier($**) we are allowed to create multiple text indexes fields. Due to the wildcard text index MongoDB indexes each and every field that holds string data in all the documents present in the given collection.
Wildcard text index is useful for unstructured data where we don't know which field contains string data or for ad-hoc query. It allowed searching text on all the fields that contain string data. Wild text index can be part of the compound index.
MongoDB Wildcard Index Example
Here, we create the text indexes using a wildcard specifier
db.studentsposts.createIndex( { "$**": "text" } )
Output:

Now, we will see all the indexes present in the studentsposts collection.

Limitations of MongoDB Text Indexes
While text indexes are powerful, there are some important limitations to be aware of:
- Only One Text Index per Collection: MongoDB allows only one text index per collection.
- No Hint with $text: We cannot use the
hint()
method with a$text
query to optimize query performance. - Text Index and Sorting Limitation: We cannot combine text index queries with sorting, as the results may be inconsistent.
- No Multi-Key or Geospatial Indexes in Compound Text Indexes: A compound text index cannot include multi-key or geospatial index fields.
Important Points About MongoDB Text Indexes
- The text index feature in MongoDB provides a powerful way to search and query textual data stored in your collections
- To create a text index in MongoDB, use the
createIndex()
method and specify the field(s) to be indexed with the"text"
type. This allows you to perform full-text searches on the indexed fields. - We can assign weights to the indexed fields to indicate their relative significance for the text search score.
- We can index multiple fields in a single text index. MongoDB also supports compound indexes that include a text index key along with other index keys.
- To drop a text index, use the
dropIndex()
method and specify the index name.
Conclusion
MongoDB text indexes offer a powerful and efficient way to search text-based data stored in your collections. With full-text search capabilities, weighted fields, and support for wildcard indexes, MongoDB makes it easy to perform complex text searches on structured or unstructured data. Understanding the limitations and proper use of text indexes ensures that you get the most out of your search queries.