MongoDB Text Indexes

The MongoDB Text Indexes feature allows users to perform full-text searches on text fields in a collection efficiently. It supports text search queries, enabling users to search for specific words or phrases within text fields.

Create Text Indexes in MongoDB

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.

You 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 (ascending or descending).

To drop a text index, simply use the index name with the dropIndex() method.

Syntax

db.collectionName.createIndex( { field: “text” } )

MongoDB Text Indexes Example

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 Index

Using wildcard specifier($**) you 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 Text Index

  • At most one text index is allowed per collection
  • With $text query expression, we cannot use hint()
  • Together Text Index and Sort cannot give the required results. The sort operations cannot use the ordering in the text index.
  • Compound Index cannot include multi-key or geospatial index fields.

Important Points MongoDB About 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.
  • You can assign weights to the indexed fields to indicate their relative significance for the text search score.
  • You 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.