w3resource

MongoDB Evaluation Query operator - $regex

Description

A regular expression is an important way of specifying a pattern for a complex search. The MongoDB $regex operator provides such a way to search a string after matching a specified pattern.

In MongoDB, matching the pattern can do by using regular expression objects or using the $regex operator.

Our database name is 'myinfo' and our collection name is 'student'. Here, is the collection bellow.

Sample collection "student"

[
        {
                "f_name" : "Zenny",
                "sex" : "Female",
                "class" : "VI",
                "age" : 12,
                "grd_point" : 32.6342
        },
        {
                "f_name" : "Paul",
                "sex" : "Male",
                "class" : "VII",
                "age" : 13,
                "grd_point" : 29.5904
        },
        {
                "f_name" : "Tom",
                "sex" : "Male",
                "class" : "VI",
                "age" : 11,
                "grd_point" : 30.1257
        },
        {
                "f_name" : "Lassy",
                "sex" : "Female",
                "class" : "VIII",
                "age" : 13,
                "grd_point" : 28.2514
        },
        {
                "f_name" : "Peter",
                "sex" : "Male",
                "class" : "VI",
                "age" : 11,
                "grd_point" : 31.5201
        }
]	
		

Example of MongoDB Evaluation Query operator - $regex

If we want to select all documents from the collection "student" which satisfying the condition -

The f_name must start with letter "P"

the following mongodb command can be used :

>db.student.find( { f_name: { $regex: 'P.*'} } ).pretty();

N.B. find() method displays the documents in a non structured format but to display the results in a formatted way, the pretty() method can be used.

SQL equivalent command is

SELECT * 
FROM student 
WHERE f_name LIKE 'P%';

Output:

{
        "_id" : ObjectId("527b3cc65ceafed9b2254a95"),
        "f_name" : "Paul",
        "sex" : "Male",
        "class" : "VII",
        "age" : 13,
        "grd_point" : 29.5904
}
{
        "_id" : ObjectId("527b3cc65ceafed9b2254a98"),
        "f_name" : "Peter",
        "sex" : "Male",
        "class" : "VI",
        "age" : 11,
        "grd_point" : 31.5201
}

If we write the above mongodb statement like below no output will be available.

>db.student.find( { f_name: { $regex: 'p.*'} } ).pretty();

Because no f_name is starting with letter 'p'. In that case to avoid the case sensetivity we can write the above statement in following manner. Here is the example -

>db.student.find( { f_name: { $regex: 'p.*', $options: 'i' } } ).pretty();

The option "$option : i" is used to avoid the case sensitivity.

The above statement can be wirtten without using $regex but using regular expression objects. Here are the commands -

>db.student.find( { f_name: /p/i } ).pretty();
>db.student.find( { f_name: /P/ } ).pretty();
>db.student.find( { f_name: /^p/i } ).pretty();

Previous: $mod
Next: $where



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mongodb/mongodb-regex-operators.php