MongoDB - Regular Expression

Regular Expressions are one of the precious components of programming that verifies different collection of patterns, especially in the form of strings. Regular Expressions abbreviated as regex are used as part of different languages for searching the patterns or words in a collection of strings. In this chapter, you will learn about the regular expression in MongoDB.

What Are Regular Expressions in MongoDB?

Regular expressions are a pattern matching technique provided by MongoDB as a feature that is used to search for a string within documents. You may encounter a situation where you may know what exact field value you are searching for. So, in such scenarios, regular expressions help in assisting and retrieving datasets based on pattern matches while searching.

Technically, MongoDB makes use of the PCRE (Perl Compatible Regular Expression) as its regular expression language for implementing regex features. Contrasting to that of text search, in MongoDB, you do not need to perform any configuration for using regular expressions.

Use of $regex Operator for Pattern Matching

The $regex operator of MongoDB is implemented for searching any specific string from the collection. Here is an example that shows how it is usually done.

Consider a dataset which has your Student collection containing the Field names as "StudentName".:

Here in the code mentioned below, you can use a regex operator like this for specifying the search criteria.

Example:
db.Student.find( {StudentName : {$regex: "Es" }} ).forEach(printjson)
Output:


Here you are trying to find all students' names that have the characters 'Es' in it. So, you can make use of the $regex operator for defining the search criteria of 'Es'. Again, the "printjson" is implemented for printing each document that gets returned by your search query in an enhanced manner.

Use of Regex Expression Having Case Insensitive

For making the searching case insensitive, you have to make use of the $options factor with the value $i. Here is a code snippet that will try to find strings with the word "Esha", irrespective of the cases:

Example:
db.Student.find( {StudentName : {$regex : "es", $options:"$i"} }).forEach(printjson)

Pattern Matching with $options

As you make use of the regex operator, you can also offer additional options by implementing the $options as a keyword. Let us consider a scenario, where you wish to locate all the documents having 'es' within their Student Name, avoiding the case sensitivity or insensitivity. In such a scenario, you have to make use of the $options.

Example:
db.Student.find({StudentName:{$regex: "es", $options:'i'} }).forEach(printjson)

Pattern Matching Performed Without the $regex Operator

There is another way to do pattern matching, which is without the regex operator. Here is a code snippet that shows how to implement regex operation without using the $regex operator.

Example:
db.Student.find({ StudentName: /Es/}).forEach(printjson)

Here, the "//" options mainly designate a MongoDb operation that is for specifying your searching standard within the mentioned delimiters.

Therefore, the above statement or command is also looking for the name of the student, which has the string "Es" within the student name of the student document.

Optimizing Regular Expression Queries

  • When your document fields indexed, the search query will make use of those indexed values for matching the regular expression.
  • In case, the regular expression you have implemented is prefix expression; then all the matched elements are meant to begin with assured string characters.

Scroll Back to Top