MongoDB Regular Expression $regex with Example

An In-depth Look at Regular Expression $regex in MongoDB:

Projection in MongoDB was explained in detail in our previous tutorial.

This tutorial will explain about Regular Expressions in MongoDB in detail. We can define regular expression as finding a pattern or word in any string. The regular expression is useful in almost all kind of languages.

Check out the Complete MongoDB training series for a clear understanding of the concept.

Regular Expression in MongoDB

Regular expression is a useful functionality of the MongoDB. When we talk about MongoDB, it uses PCRE (pearl compatible regular expression) as a regular expression. It uses $regex operator as a regular expression for finding patterns in a string.


Consider the following example in which the document is structured under the collection of the “softwaretestinghelp”.

Figure 1

Example of Regular Expression in Mongodb



Method 1:



The above query search is to look for all the students containing the name “Junaid”.

Figure 2: Results in MongoDB Shell

Results in MongoDB Shell

Figure 3: Results in Robo3T

Results in Robo3T:

From the above example, we found that regular expression uses to find patterns or word in a particular String.



The query will also return the same result. You can take a look at the results below.

Figure 4: Results in MongoDB Shell

pretty()- Results in MongoDB Shell

Figure 5: Results in Robo3T

pretty()- Results in robo 3t

How will Regular Expression Work in Case-sensitive Situation?

For a case-sensitive situation, regular expression uses $option and the parameter with a value of $i. Here we are giving an example in which the regular expression works in a case-sensitive situation. The following query will return the value containing “Junaid” irrespective of smaller or capital words.



Figure 6: Results in MongoDB Shell

case-sensitive situation- Results in MongoDB Shell

Figure 7: Results in Robo3T

case-sensitive situation-Results in Robo3T

How can we use a Regular Expression in an Array?

We can use the concept of regular expression in an array. Regular expression is very important when we are working with the tags. If you want to search all the tags with ” my new post” the following query will find all the Post having tags “my new post” and return us a result.



Figure 8: Results in MongoDB Shell

regular expression in an array-Results in MongoDB Shell

Figure 9: Results in Robo3T

regular expression in an array-Results in Robo3T

Regular Expression Optimization

  • If we have a document in index form, the query will use the values of indexes and match with the regular expression.
  • If the regular expression is a prefix expression, then the query will find all the values starting from a specific word.
  • For Example, if a word starts from ‘st’ the query will find all the values starting from the word st.


To summarize, we learned the following points from the above tutorial:

  • Regular expression and its uses.
  • Regular expression in a case-sensitive situation and optimization of a regular expression.
  • Using regular expression in an array.

From the above study, we can say that the regular expression is used for pattern and word finding from the specified string. A regular expression is able to search an indexed document by matching the values to the indexed values. If the regular expression is in a prefix form, then it will search out all the results starting from a specific prefix.

Watch out our upcoming tutorial to know more about Sharding in MongoDB.

PREV Tutorial | NEXT Tutorial