MongoDB Text Search - MongoDB

How text search works in MongoDB?

Starting from version 2.4, MongoDB has been supporting text indexes to search inside the string content. Text Search makes use of stemming techniques to search for specified words in the string fields by dropping stemming stop words like a, an, the, etc. Currently, MongoDB supports around 15 languages.

Enabling Text Search

Initially text Search was an experimental feature, but starting from version 2.6, the configuration is enabled by default. But for the previous version of MongoDB, text search should be enabled with below code

Creating Text Index

Consider below document under posts collection which consists of the post text and its tags

Let’s create a text index on post_text field so that we can search inside our posts' text

Using Text Index

Now that we have created the text index on post_text field, let’s search for all the posts which have the word wisdomjobs in their text.

Above command returns below result documents having the word wisdomjobs in their post text

For older versions of MongoDB, use below command

Using Text Search highly improves the search efficiency when compared to normal search.

Deleting Text Index

To delete an existing text index, first find the name of index using below query

After getting the name of the index from above query, run below command. Here, post_text_text is the name of the index.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

MongoDB Topics