Javatpoint Logo
Javatpoint Logo

MongoDB Regular Expression ($regex)

In MongoDB, the regular expressions are used for pattern matching, which is basically for searching patterns in a string within documents. It is a generalized way of matching a pattern to a sequence of characters. The $regex operator is used as a regular expression to find a pattern in a string.

In MongoDB, we can do pattern matching in two different ways:

  1. Using $regex operator for Pattern matching
  2. Pattern matching without the $regex operator

Using $regex operator for Pattern matching

In MongoDB, the $regex operator provides the functionality for pattern matching in the queries. In other words, the $regex operator is used to search a specific string in the documents.

Note: It is not allowed to use the $in operator inside the $regex operator.

Syntax:

$options:

The following <option> can be used with regular expressions:

  1. The "i" character is used to match both lower case and upper case patterns in a string.
  2. The "^" and "$" symbols are used to search for a specific pattern in the document. The "^" symbol is used to ensure that the string will begin with a certain character, and $ is used to ensure that the string will end with a certain character.
  3. The "x" character is used to ignore all white space characters in the string.

Examples:

In the following examples, we are working with:

>db.student.find()
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
           "name" : "Mick",
           "Course" : "btech",
           "batch_year" : 2018,
           "language" : ["c++", "java", "python"],
           "personal_details" : 
                     {
                      "Father_name" : "Jonny",
                      "phone_no" : 8895321456,
                      "age" : 23,
                      "gender" : "Male",
                      "City" : "NewYork",
                     }            
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"),
           "name" : "MIck99",
           "Course" : "BCA",
           "batch_year" : 2020,
           "language" : ["C#", "JavaScript"],
           "personal_details" : 
                     {
                      "Father_name" : "Henry",
                      "phone_no" : 9874563698,
                      "age" : 20,
                      "gender" : "Female",
                      "City" : "London",
                     }
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d655"),
           "name" : "Jonny",
           "Course" : "MCALE",
           "batch_year" : 2019,
           "language" : ["C#", "java", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "Thomas",
                      "phone_no" : 7845123698,
                      "age" : 24,
                      "gender" : "Male",
                      "City" : "London",
                     }          
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d678"),
           "name" : "Oliver",
           "Course" : "MCA",
           "batch_year" : 2017,
           "language" : ["c", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "William",
                      "phone_no" : 9997845123,
                      "age" : 25,
                      "gender" : "Male",
                      "City" : "Liverpool",
                     }           
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"),
           "name" : "mia",
           "Course" : "btech",
           "batch_year" : 2020,
           "language" : ["HTML", "CSS", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "Leo",
                      "phone_no" : 6312547896,
                      "age" : 22,
                      "gender" : "Female",
                      "City" : "Manchester",
                     }           
}

Example 1: Display the details of students who are pursuing B.Tech.

We are displaying the details of students whose course is btech, so we pass a regular expression using the $regex operator (i.e. {$regex : "btech"}) for the course field in the find() method.

Note: Printjson is being used to print each document that is returned by the query better.

Output:

>db.student.find({Course : {$regex: "btech" }}).forEach(printjson)
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
           "name" : "Mick",
           "Course" : "btech",
           "batch_year" : 2018,
           "language" : ["c++", "java", "python"],
           "personal_details" : 
                     {
                      "Father_name" : "Jonny",
                      "phone_no" : 8895321456,
                      "age" : 23,
                      "gender" : "Male",
                      "City" : "NewYork",
                     }            
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"),
           "name" : "mia",
           "Course" : "btech",
           "batch_year" : 2020,
           "language" : ["HTML", "CSS", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "Leo",
                      "phone_no" : 6312547896,
                      "age" : 22,
                      "gender" : "Female",
                      "City" : "Manchester",
                     }           
}

Example 2: Displaying details of students whose name starts with "Mi" using i <options>.

Note: Printjson is being used to print each document that is returned by the query better.

Output:

>db.student.find({name:{$regex: "Mi",$options:'i'}}).forEach(printjson)
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
           "name" : "Mick",
           "Course" : "btech",
           "batch_year" : 2018,
           "language" : ["c++", "java", "python"],
           "personal_details" : 
                     {
                      "Father_name" : "Jonny",
                      "phone_no" : 8895321456,
                      "age" : 23,
                      "gender" : "Male",
                      "City" : "NewYork",
                     }            
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"),
           "name" : "MIck99",
           "Course" : "BCA",
           "batch_year" : 2020,
           "language" : ["C#", "JavaScript"],
           "personal_details" : 
                     {
                      "Father_name" : "Henry",
                      "phone_no" : 9874563698,
                      "age" : 20,
                      "gender" : "Female",
                      "City" : "London",
                     }
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"),
           "name" : "mia",
           "Course" : "btech",
           "batch_year" : 2020,
           "language" : ["HTML", "CSS", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "Leo",
                      "phone_no" : 6312547896,
                      "age" : 22,
                      "gender" : "Female",
                      "City" : "Manchester",
                     }           
}

Example 3: Displaying details of students whose course is MCA.

Note: Printjson is being used to print each document that is returned by the query better.

Output:

>db.student.find({Course : {$regex: "^MCA$"}}).forEach(printjson) 
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d678"),
           "name" : "Oliver",
           "Course" : "MCA",
           "batch_year" : 2017,
           "language" : ["c", "PHP"],
           "personal_details" : 
                     {
                      "Father_name" : "William",
                      "phone_no" : 9997845123,
                      "age" : 25,
                      "gender" : "Male",
                      "City" : "Liverpool",
                     }           
}

Pattern matching without the $regex operator

In MongoDB, we can do pattern matching without using the $regex operator. In this method, it is used regular expression objects.

Syntax:

Example 4: Displaying details of students whose name contains "ck" using a regular expression object.

The "//" means specifying your search criteria amonug these delimiters. So, we are looking for documents that have 'CK' in their name.

Output:

{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
           "name" : "Mick",
           "Course" : "btech",
           "batch_year" : 2018,
           "language" : ["c++", "java", "python"],
           "personal_details" : 
                     {
                      "Father_name" : "Jonny",
                      "phone_no" : 8895321456,
                      "age" : 23,
                      "gender" : "Male",
                      "City" : "NewYork",
                     }            
}
{
           "_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"),
           "name" : "MIck99",
           "Course" : "BCA",
           "batch_year" : 2020,
           "language" : ["C#", "JavaScript"],
           "personal_details" : 
                     {
                      "Father_name" : "Henry",
                      "phone_no" : 9874563698,
                      "age" : 20,
                      "gender" : "Female",
                      "City" : "London",
                     }
}






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA