MongoDB has gained immense popularity among technical companies owing to its flexibility and easy usage. The internet has ample big data and complexities. A NoSQL document-oriented database like MongoDB efficiently handles and sorts all unstructured and complicated data. MongoDB version 2.4 introduced text indexes to search inside string content.  

The Ultimate Ticket to Top Data Science Job Roles

Post Graduate Program In Data ScienceExplore Now
The Ultimate Ticket to Top Data Science Job Roles

What Is Text Search in MongoDB?

Text search is a MongoDB technique used to find a piece of text from the string fields. This query operation helps in finding a specific word from the string. One can perform a text search utilizing the text index and $text operator.

The text search employs stemming techniques while hunting for words. It drops stemming stop words such as a, an, the, and others. MongoDB supports over 15 languages and allows users to look for specific texts efficiently.

What Is a Text Index?

A text index in MongoDB is used to find the specified text in a string content. It is either in the form of a string or an array of string elements. To perform a text search query, one must ensure that the collection contains only one text index covering multiple fields. You can use the createIndex() method to create a text index. 

Syntax:

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

What Is a $text Operator? 

Using a $text operator, one can search a text index. The operator enables text search operations on the collection with a text index. It tokenizes every search string with whitespace, treating most punctuations as delimiters except for – and \”. The operator then performs logical OR operations on the tokens. Using the $meta query operator, you can sort the resultant documents.  

Syntax:

$text:

{

     $search: <string>,

     $language: <string>,

     $caseSensitive: <boolean>,

     $diacriticSensitive: <boolean>

}

Get In-Demand Skills to Launch Your Data Career

Big Data Hadoop Certification Training CourseExplore Course
Get In-Demand Skills to Launch Your Data Career

When the feature was initially introduced on an experimental basis, it had to be enabled, but from version 2.6, it is enabled by default. Use the following steps to search texts in MongoDB. 

Creating a Collection 

  • Begin by creating a collection. Add a few documents to the collection. The following example considers a document under post collection. It contains post text and its tags.

> db.posts.insert({

   "post_text": "learn mongodb with simplilearn",

   "tags": ["mongodb", "simplilearn"]

}

{

"post_text" : "mongodb detailed tutorials",

"tags" : [ "mongodb", "tutorial" ]

})

WriteResult({ "nInserted" : 1 })

Creating a Text Index

Now create a text index on the post_text field to search inside the posts' text.

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

{

"createdCollectionAutomatically" : true,

"numIndexesBefore" : 1,

"numIndexesAfter" : 2,

"ok" : 1

}

How to Use a Text Index?

Once you have created the text index, search for all the posts having the word Simplilearn in their text.

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

{

"_id" : ObjectId("5dd7ce28f1dd4583e7103fe0"),

"post_text" : "learn mongodb with simplilearn",

"tags" : [

"mongodb",

"simplilearn"

]

}

This command returns the following result with the word Simplilearn in the post text.

{

"_id" : ObjectId("53493d14d852429c10000002"),

"post_text" : "learn mongodb with simplilearn",

"tags" : [ "mongodb", "simplilearn" ]

}

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

How to Delete a Text Index? 

For deleting a text index, use the following query to find the name of the index:

>db.posts.getIndexes()

[

{

"v" : 2,

"key" : {

"_id" : 1

},

"name" : "_id_",

"ns" : "mydb.posts"

},

{

"v" : 2,

"key" : {

"fts" : "text",

"ftsx" : 1

},

"name" : "post_text_text",

"ns" : "mydb.posts",

"weights" : {

"post_text" : 1

},

"default_language" : "english",

"language_override" : "language",

"textIndexVersion" : 3

}

]

>

On obtaining the name of the index, run the command given below to delete it. 

Note: “post_text_text” in the following command refers to the name of the index.

>db.posts.dropIndex("post_text_text")

{ "nIndexesWas" : 2, "ok" : 1 }

Simplilearn's Professional Certificate Program in Data Science in partnership with Purdue University and in collaboration with IBM, is ranked #1 Post Graduate in Data Science program by ET. If you wish to ace data science, this program is just the one for you!

Choose the Right Program

Accelerate your career with Simplilearn's Data Science courses! Choose the right program tailored to your needs and become a data wizard. Gain in-demand skills, learn from industry experts, and unlock exciting job opportunities. Don't miss out on this chance to become a sought-after data scientist. Enroll today!

Program Name

DS Master's

Post Graduate Program In Data Science

Professional Certificate Course In Data Science

Geo All Geos IN/ROW IN
University Simplilearn Caltech IIT Kanpur
Course Duration 11 Months 11 Months 11 Months
Coding Experience Required Basic No Yes
Skills You Will Learn 10+ skills including data structure, data manipulation, NumPy, Scikit-Learn, Tableau and more 8+ skills including
Supervised & Unsupervised Learning
Deep Learning
Data Visualization, and more
8+ skills including
NLP, Data Visualization, Model Building, and more
Additional Benefits Applied Learning via Capstone and 25+ Data Science Projects Upto 14 CEU Credits Caltech CTME Circle Membership Live masterclasses from IIT Kanpur faculty and certificate from E&ICT Academy, IIT Kanpur
Cost $$ $$$$ $$$
Explore Program Explore Program Explore Program

Conclusion

Pursue opportunities and advance your skills to become an expert in Data Science. Enroll in Professional Certificate Program in Data Science to gain valuable insights from IBM experts and Purdue faculty. The course is created by industry experts incorporating proven methods that have helped thousands of others like you master the field. 

Our Big Data Courses Duration And Fees

Big Data Courses typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Post Graduate Program in Data Engineering

Cohort Starts: 5 Apr, 2024

8 Months$ 3,850