Open In App

MongoDB – Regex

MongoDB provides the functionality to search a pattern in a string during a query by writing a regular expression. A regular expression is a generalized way to match patterns with sequences of characters. MongoDB uses Perl compatible regular expressions(PCRE) version 8.42 along with UTF-8 support. In MongoDB, we can do pattern matching in two different ways:

Pattern matching using $regex operator

This operator provides regular expression capabilities for pattern matching stings in the queries. Or in other words, this operator is used to search for the given string in the specified collection. It is helpful when we don’t know the exact field value that we are looking in the document. For example, a collection containing 3 documents i.e.,



{
name: "Tony",
position: "Backend developer"
}  
{
name: "Bruce",
position: "frontend developer"
}  
{
name: "Nick",
position: "HR Manager"
} 

and we are looking for developer information. So, with the help of the $regex operator, we create a pattern(i.e., {position: {$regex: “developer”}}) that will return only those documents that contain developer string. 

Important Points:



Syntax:

{ <field>: { $regex: /pattern/, $options: '<options>' } }
{ <field>: { $regex: 'pattern', $options: '<options>' } }

$options:

In MongoDB, the following <options> are available for use with regular expression:

Examples:

In the following examples, we are working with:

Database: GeeksforGeeks

Collection: employee

Documents: Six documents that contain the details of the employees in the form of field-value pairs.

db.employee.find({position : {$regex : "developer"}}).pretty()

Here, we are displaying the documents of those employees whose position field contain developer string. So we pass a regular expression using $regex operator(i.e. {$regex : “developer”}) for the position field in the find() method.

db.employee.find({position:{$regex:"software",$options:"i"}}).pretty()

Here, we are displaying the documents of those employees whose position field contain case-insensitive “software” string. So, we pass a regular expression with option(i.e., {$regex : “software”, $options: “$i”}) for the position field in the find() method. In the regular expression, $options:”$i” is used to match both lower case and upper case pattern in the given string(i.e., “software”).

db.employee.find({Name:{$regex:"^B"}}).pretty()

Here, we are displaying the documents of those employees whose name starts with ‘B’ letter. So, we pass a regular expression using $regex operator(i.e. {$regex : “^B”}) for the Name field in the find() method.

db.employee.find({Name:{$regex:"e$"}}).pretty()

Here, we are displaying the documents of those employees whose names end with the ‘e’ letter. So, we pass a regular expression using $regex operator(i.e. {$regex : “e$”}) for the Name field in the find() method.

Pattern matching without using $regex operator

In MongoDB, we can do pattern matching without using the $regex operator. Simply, by using a regular expression object to specify a regular expression. Also, by using regular expression object you are allowed to use regular expression inside $ in operator.

Syntax: 

{ <field>: /pattern/<options> }

Here, // means to specify your search criteria in between these delimiters.

Example:

In the following examples, we are working with:

Database: GeeksforGeeks

Collection: employee

Documents: Six documents that contain the details of the employees in the form of field-value pairs.

db.employee.find({Name: /te/}).pretty()

Here, we are displaying the documents of those employees whose names contain the “te” string. So, we pass a regular expression(i.e., {Name: /te/}) for the Name field in the find() method. In this regular expression, // means to specify your search criteria in between these delimiters, i.e., /te/.


Article Tags :