MongoDB Regular Expressions
Regular expressions are used to describe and match a series of strings that conform to a certain syntactic rule using a single string.
Many programming languages support string operations using regular expressions.
MongoDB uses the $regex operator to set the regular expression for matching strings.
MongoDB uses PCRE (Perl Compatible Regular Expression) as its regular expression language.
Unlike full-text search, we do not need to configure anything to use regular expressions.
Consider the following posts collection document structure, which includes the article content and tags:
{
"post_text": "enjoy the mongodb articles on tutorialpro",
"tags": [
"mongodb",
"tutorialpro"
]
}
Using Regular Expressions
The following command uses a regular expression to find articles containing the string "tutorialpro":
>db.posts.find({post_text:{$regex:"tutorialpro"}})
The above query can also be written as:
>db.posts.find({post_text:/tutorialpro/})
Case-Insensitive Regular Expressions
If the search needs to be case-insensitive, we can set $options to $i.
The following command will find the string "tutorialpro" without case sensitivity:
>db.posts.find({post_text:{$regex:"tutorialpro",$options:"$i"}})
This will return all documents containing the string "tutorialpro" regardless of case:
{
"_id" : ObjectId("53493d37d852429c10000004"),
"post_text" : "hey! this is my post on tutorialpro",
"tags" : [ "tutorialpro" ]
}
Using Regular Expressions on Array Elements
We can also use regular expressions on array fields to find content. This is particularly useful for tags. If you need to find data with tags starting with "run" (e.g., "ru", "run", or "tutorialpro"), you can use the following code:
>db.posts.find({tags:{$regex:"run"}})
Optimizing Regular Expression Queries
- If an indexed field is present in your documents, using the index is faster than matching all data with a regular expression.
- If the regular expression is a prefix expression, all matching data will start with the specified prefix string. For example, if the regular expression is ^tut, the query will find strings starting with "tut".
There are two things to note when using regular expressions:
Using variables in regular expressions. You must use eval to convert the combined string; you cannot directly concatenate the string and pass it to the expression. Otherwise, there will be no error message, and the result will be empty! Here is an example:
var name=eval("/" + variableValueKey +"/i");
The following is a fuzzy query for the keyword "title", case-insensitive:
title:eval("/"+title+"/i") // Equivalent to title:{$regex:title,$Option:"$i"}