Learn How to Use Regular Expressions in MongoDB

Regular Expression

Regular Expression

In this article, we are going to discuss how to Use Regular Expressions in MongoDB. A regular expression, also known as regex or regexp is a sequence of characters that define a particular search pattern in the formal language theory of theoretical computer science. It is used frequently to search for a particular pattern or the words in a string. The MongoDB has the $regex operator which provides this functionality of regular expression for string pattern matching. MongoDB uses a regular expression language known as PCRE (PCRE acronym, stands for Perl Compatible Regular Expression). In MongoDB, there is no need to configure or command like text search in order to use regular expressions.

Let’s consider a simple example of the following document structure, for the blog collections containing the blog text and the associated tags to search text by using the regular expression query.

Using regex Expression
We can use regular expression to search for the text “MongoDB” from the blog_text field as shown below.

Alternatively, we can re-write the above regex query in the following way. It will generate the same output as above.

How to use regex Expression with Case Insensitive string in MongoDB?
In MongoDB, we can search for a string in the case insensitive manner by using the $options parameter with its value as $i. In the above example, if we want to search the blog_text for the string mongodb in the case insensitive manner, then we have rephrase the query by using the $options parameter with value $i. An example is shown below.

We can use the pretty method to look our result in a presentable manner as shown below.

How to use regex for Array Elements in MongoDB?
In MongoDB, the concept of regex can also be used on the array fields. When we implement the functionality of tags then it is very important to have tags beginning from the word MongoDB (either Mongo or MongoDB) in order to search for all the posts having these tags. We can use the following code.

Regular Expression Queries Optimization in MongoDB
Like the relational databases, we can optimize regular expression queries in the MongoDB. We need to apply certain database tricks in order to optimize the data retrieval from the MongoDB. The following are some of the concepts used in the MongoDB for the query optimization.

• We can create database indexes on the document fields so that the MongoDB query will make use of these indexed values to match the regular expression. This makes the search and the data retrieval from the collection very fast as compared to the traditional regular expression scanning that scan the collection as a whole.
• Create a query with the regular expression as a prefix expression where all the matches are meant to begin with a particular string characters. For e.g., if the regex expression is ^Mongo, then such a query will search only for those strings that begin with Mongo.

Conclusion: –
In this chapter, we discussed the use of Regular Expression in the MongoDB. Here, we have learnt along with supporting examples about the use of regular expressions in MongoDB for both case sensitive and case insensitive manner and with the array elements. We have also touched the topic on the MongoDB regular expression queries optimization.


Please enter your comment!
Please enter your name here