Regular Expressions are frequently used in all languages to search for a pattern or word in any string. MongoDB also provides the functionality of regular expression for string pattern matching using the $regex operator. MongoDB uses PCRE (Perl Compatible Regular Expression) as a regular expression language.
Unlike text search, we do not need to do any configuration or command to use regular expressions.
Assume we have inserted a document in a database named posts as shown below −
> db.posts.insert(
{
"post_text": "enjoy the mongodb articles on adglob",
"tags": [
"mongodb",
"adglob"
]
}
WriteResult({ "nInserted" : 1 })
Using regex Expression
The following regex query searches for all the posts containing string adglob in it −
> db.posts.find({post_text:{$regex:"adglob"}}).pretty()
{
"_id" : ObjectId("5dd7ce28f1dd4583e7103fe0"),
"post_text" : "enjoy the mongodb articles on adglob",
"tags" : [
"mongodb",
"adglob"
]
}
{
"_id" : ObjectId("5dd7d111f1dd4583e7103fe2"),
"post_text" : "enjoy the mongodb articles on adglob",
"tags" : [
"mongodb",
"adglob"
]
}
>
The same query can also be written as −
>db.posts.find({post_text:/adglob/})
Using regex Expression with Case Insensitive
To make the search case insensitive, we use the $options parameter with value $i. The following command will look for strings having the word adglob, irrespective of smaller or capital case −
>db.posts.find({post_text:{$regex:"adglob",$options:"$i"}})
One of the results returned from this query is the following document which contains the word adglob in different cases −
{
"_id" : ObjectId("53493d37d852429c10000004"),
"post_text" : "hey! this is my post on Adglob",
"tags" : [ "adglob" ]
}
Using regex for Array Elements
We can also use the concept of regex on the array field. This is particularly very important when we implement the functionality of tags. So, if you want to search for all the posts having tags beginning from the word tutorial, you can use the following code −
>db.posts.find({tags:{$regex:"tutorial"}})
Optimizing Regular Expression Queries
- If the document fields are indexed, the query will use make use of indexed values to match the regular expression. This makes the search very fast as compared to the regular expression scanning the whole collection.
- If the regular expression is a prefix expression, all the matches are meant to start with a certain string characters. For e.g., if the regex expression is ^tut, then the query has to search for only those strings that begin with tut.