Skip to content
Related Articles

Related Articles

Improve Article

MongoDB – Text Indexes

  • Last Updated : 17 Feb, 2021

MongoDB is a document-based NoSQL database. As the data is stored in the format of the document, it can hold a huge amount of data and as it is a NoSQL database type, there is no strict necessity to have referential integrity relationships. So searching is an important criteria here and for that MongoDB provides Text indexes to support text search queries especially on string content. Text indexes should be either a string or an array of string elements.

How to create text indexes?

In MongoDB, we can create text indexes using db.collectionName.createIndex() method. So, to index a field that contains either string or an array of string elements, pass a document in the createIndex() method that contains the field and the string literal(i.e., “text”). Using this method you are allowed to index multiple fields for the text index. Also, a compound index can contain text index key in combination with ascending and descending index key. And if you want to drop a text index, just use the index name.

Syntax:

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

Example:



Database: ggf

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.

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

Now we will see how to search using Text Index:

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



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

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:

db.collection.dropIndex("TextIndex")

Example:

First, we find the index of the field.

db.studentsposts.getIndexes()

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

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.

Example:

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

Here, we create the text indexes using a wildcard specifier.

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

Limitations

  • 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.
My Personal Notes arrow_drop_up
Recommended Articles
Page :