Text Search in MongoDB

Starting with version 2.4, MongoDB started supporting Text Indexes to search within the string content. Text Search uses Stemming technique to search for specific words in string fields by deleting Stemming Word such as a, an, the, . Currently, MongoDB supports about 15 languages.

Enable Text Search in MongoDB

At first, Text Search was an experimental feature, but starting with version 2.6, the configuration is enabled by default. But if you are using a previous version of MongoDB, you need to activate Text Search with the following code:

 > db . adminCommand ({ setParameter : true , textSearchEnabled : true }) 

Create Text Index in MongoDB

You follow the following Document under the collection posts containing post_text and its tags.

 { "post_text" : "enjoy the mongodb articles on tutorialspoint" , "tags" : [ "mongodb" , "tutorialspoint" ] } 

We will create a Text Index on the post_text field so that we can search within the text section of posts:

 > db . posts . ensureIndex ({ post_text : "text" }) 

Use Text Index in MongoDB

Now that you have created the Text Index on the post_text field, we will search all posts that are from tutorialspoint in their text:

 > db . posts . find ({ $text :{ $search : "tutorialspoint" }}) 

The above command returns Documents with tutorialspoint in post_text:

 { 
"_id": ObjectId ("53493d14d852429c10000002"),
"post_text": "enjoy the mongodb articles on tutorialspoint",
"tags": ["mongodb", "tutorialspoint"]
}
{
"_id": ObjectId ("53493d1fd852429c10000003"),
"post_text": "writing tutorials on mongodb",
"tags": ["mongodb", "tutorial"]
}

If you are using an older version of MongoDB, you must use the following command:

 > db . posts . runCommand ( "text" ,{ search : " tutorialspoint " }) 

The use of Text Search significantly improves search efficiency when compared to regular search.

Delete Text Index in MongoDB

To delete an existing Text Index, first find the index's name using the query:

 > db . posts . getIndexes () 

After taking the name of the index from the above query, run the following command. Here, post_text_text is the name of the index.

 > db . posts . dropIndex ( "post_text_text" ) 

According to Tutorialspoint

Previous article: Map Reduce in MongoDB

Next post: Regular Expression in MongoDB

4 ★ | 2 Vote

May be interested

  • Shard in MongoDBShard in MongoDB
    sharding is a process of storing data records across multiple devices and it is a method of mongodb to meet the requirement for increasing data. when the size of the data increases, a single device cannot be enough to store data.
  • Learn about Java Driver in MongoDBLearn about Java Driver in MongoDB
    in the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
  • Install MongoDBInstall MongoDB
    instructions for installing mongodb on windows.
  • Map Reduce in MongoDBMap Reduce in MongoDB
    in mongodb documentation, map-reduce is a data processing system that condenses a large amount of data into useful overall results. mongodb uses mapreduce command for map-reduce operation. in general, map reduce is used to handle large data sets.
  • How to Search for Text in Files on WindowsHow to Search for Text in Files on Windows
    this wikihow teaches you how to search for specific text inside any document on your windows pc. press . this opens the windows search box.
  • Aggregation in MongoDBAggregation in MongoDB
    aggregation can be understood as aggregation. the aggregation operation handles data records and returns calculated results. the operations group the values ​​from multiple documents together, and can perform multiple operations on the grouped data to return a single result. in sql, count (*) and group by are equivalent to aggregation in mongodb.
  • Data modeling in MongoDBData modeling in MongoDB
    data in mongodb has a flexible schema. documents in the same collection need not have the same set of fields or structures, and common fields in collection documents can keep different data types.
  • How to use Aggregation Pipeline in MongoDBHow to use Aggregation Pipeline in MongoDB
    if you are using mongodb's mapreduce, it is best to switch to aggregation pipeline for more efficient computation.
  • GridFS in MongoDBGridFS in MongoDB
    gridfs is the mongodb specification for storing and collecting large files such as images, audio, video files, etc. it is a type of file system to store files but its data is stored inside mongodb collections. .
  • Query analysis in MongoDBQuery analysis in MongoDB
    analyzing queries is a very important aspect to assess the effectiveness of database and the effectiveness of the designed index. we will explore the two most frequently used queries, $ explain and $ hint.