How to use Regex Expressions and Text Search in MongoDB

Last updated on May 30 2022
Satyen Sahu

Table of Contents

How to use Regex Expressions and Text Search in MongoDB

MongoDB – Regular Expression

Regular Expressions are frequently used in all languages to search for a pattern or word in any string. MongoDB also provides functionality of regular expression for string pattern matching using the $regex operator. MongoDB uses PCRE (Perl Compatible Regular Expression) as regular expression language.
Unlike text search, we do not need to do any configuration or command to use regular expressions.
Consider the subsequent document structure under posts collection containing the post text and its tags −
{
“post_text”: “enjoy the mongodb articles on tecklearn”,
“tags”: [
“mongodb”,
“tecklearn”
]
}

Using regex Expression

The subsequent regex query searches for all the posts containing string tecklearn in it −
>db.posts.find({post_text:{$regex:”tecklearn”}})
The same query can also be written as −
>db.posts.find({post_text:/tecklearn/})

Using regex Expression with Case Insensitive

To make the search case insensitive, we use the $options parameter with value $i. The subsequent command will look for strings having the word tecklearn, irrespective of smaller or capital case −
>db.posts.find({post_text:{$regex:”tecklearn”,$options:”$i”}})
One of the results returned from this query is that the subsequent document which contains the word tecklearn in different cases −
{
“_id” : ObjectId(“53493d37d852429c10000004”),
“post_text” : “hey! this is my post on Tecklearn”,
“tags” : [ “tecklearn” ]
}

Using regex for Array Elements

We can also use the concept of regex on array field. This is particularly very important when we implement the functionality of tags. So, if you want to search for all the posts having tags beginning from the word tutorial (either tutorial or tutorials or tutorialpoint or tutorialphp), you can use the subsequent code −
>db.posts.find({tags:{$regex:”tutorial”}})

Optimizing Regular Expression Queries

• If the document fields are indexed, the query will use make use of indexed values to match the regular expression. This makes the search very fast as compared to the regular expression scanning the whole collection.
• If the regular expression is a prefix expression, all the matches are meant to start with a certain string characters. For e.g., if the regex expression is ^tut, then the query has to search for only those strings that begin with tut.

MongoDB – Text Search

Starting from version 2.4, MongoDB started supporting text indexes to search inside string content. The Text Search uses stemming techniques to look for specified words in the string fields by dropping stemming stop words like a, an, the, etc. At present, 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 if you’re using the previous version of MongoDB, you have to enable text search with the subsequent code −
>db.adminCommand({setParameter:true,textSearchEnabled:true})

Creating Text Index

Consider the subsequent document under posts collection containing the post text and its tags −
{
“post_text”: “enjoy the mongodb articles on tecklearn”,
“tags”: [
“mongodb”,
“tecklearn”
]
}
We will create a text index on post_text field so that we can search inside our posts’ text −
>db.posts.ensureIndex({post_text:”text”})

Using Text Index

Now that we have created the text index on post_text field, we will search for all the posts having the word tecklearn in their text.
>db.posts.find({$text:{$search:”tecklearn”}})
The above command returned the subsequent result documents having the word tecklearn in their post text −
{
“_id” : ObjectId(“53493d14d852429c10000002”),
“post_text” : “enjoy the mongodb articles on tecklearn”,
“tags” : [ “mongodb”, “tecklearn” ]
}
{
“_id” : ObjectId(“53493d1fd852429c10000003”),
“post_text” : “writing tutorials on mongodb”,
“tags” : [ “mongodb”, “tutorial” ]
}
If you’re using old versions of MongoDB, you have to use the subsequent command −
>db.posts.runCommand(“text”,{search:” tecklearn “})
Using Text Search highly improves the search efficiency as compared to normal search.

Deleting Text Index

To delete an existing text index, first find the name of index using the subsequent query −
>db.posts.getIndexes()
After getting the name of your index from above query, run the subsequent command. Here, post_text_text is that the name of the index.
>db.posts.dropIndex(“post_text_text”)

So, this brings us to the end of blog. This Tecklearn ‘How to use Regex Expressions and Text Search in MongoDB’ helps you with commonly asked questions if you are looking out for a job in MongoDB and No-SQL Database Domain.
If you wish to learn and build a career in MongoDB or No-SQL Database domain, then check out our interactive, MongoDB Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

MongoDB Training

MongoDB Training

About the Course

Tecklearn’s MongoDB Training helps you to master the NoSQL database. The course makes you job-ready by letting you comprehend schema design, data modelling, replication, and query with MongoDB through real-time examples. Along with this, you’ll also gain hands-on expertise in installing, configuring, and maintaining the MongoDB environment, including monitoring and operational strategies from this online MongoDB training. Upon completion of this online training, you will hold a solid understanding and hands-on experience with MongoDB.

Why Should you take MongoDB Training?

• MongoDB – a $36 billion to a $40 billion market growing at 8% to 9% annually – Forbes.com
• Average salary of a Mongo DB certified professional is $134k – Indeed.com
• MongoDB has more than 900 customers, including 27 Fortune 100 companies like Cisco, eBay, eHarmony, MetLife & Salesforce.com

What you will Learn in this Course?

Introduction to MongoDB and Importance of NoSQL
• Understanding the basic concepts of RDBMS
• What is NoSQL Database and its significance?
• Challenges of RDBMS and How NoSQL suits Big Data needs
• Types of NoSQL Database and NoSQL vs. SQL Comparison
• CAP Theorem and Implementing NoSQL
• Introduction to MongoDB and its advantages
• Design Goals for MongoDB Server and Database, MongoDB tools
• Collection, Documents and Key Value Pair
• Introduction to JSON and BSON documents
• MongoDB installation
MongoDB Installation
• MongoDB Installation
• Basic MongoDB commands and operations,
• Mongo Chef (MongoGUI) Installation
Schema Design and Data Modelling
• Why Data Modelling?
• Data Modelling Approach
• Data Modelling Concepts
• Difference between MongoDB and RDBMS modelling
• Challenges for Data Modelling
• Model Relationships between Documents
• Data Model Examples and Patterns
• Model Tree Structures
CRUD Operations
• MongoDB Architecture
• CRUD Introduction and MongoDB CRUD Concepts
• MongoDB CRUD Concerns (Read and Write Operations)
• Cursor Query Optimizations and Query Behaviour in MongoDB
• Distributed Read and Write Queries
• MongoDB Datatypes
Indexing and Aggregation Framework
• Concepts of Data aggregation and types and data indexing concepts
• Introduction to Aggregation
• Approach to Aggregation
• Types of Aggregation: Pipeline, MapReduce and Single Purpose
• Performance Tuning
MongoDB Administration
• Administration concepts in MongoDB
• MongoDB Administration activities: Health check, recovery, backup, database sharing and profiling, performance tuning etc.
• Backup and Recovery Methods for MongoDB
• Export and Import of Data from MongoDB
• Run time configuration of MongoDB
MongoDB Security
• Security Introduction
• MongoDB security Concepts and security approach
• MongoDB integration with Java and Robomongo
Got a question for us? Please mention it in the comments section and we will get back to you.

 

 

0 responses on "How to use Regex Expressions and Text Search in MongoDB"

Leave a Message

Your email address will not be published. Required fields are marked *