Regular Expression

Regular Expression

Regular expression is not something related to MongoDB it exist almost in all languages and they are used to search for a pettern or a word in a string. MongoDB provides the $regex operator to work with regular expression and to give the user a way to search for string matching a given pattern. MongoDB uses Perl compatible regular expressions (i.e. “PCRE” ) version 8.42 with UTF-8 support.

$regex syntax (PCRE)

To use $regex, we can use one of the following syntaxes:

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

$regex abbreviation using the javascript Syntax

In MongoDB, you can also use regular expression objects (i.e. /pattern/) to specify regular expressions without using the $regex operator:

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

To use PCRE supported features in the regex pattern that are unsupported in JavaScript, you must use the $regex operator expression with the pattern as a string.

$regex options

The following options are available for use with regular expression.

Option Description
i Case insensitive
m For multiline string, matches every line (used with: ^ for the start, $ for the end)
{ $regex: /^A/, $options: 'm' } matched all the strings that one of its lines starts with an A
x Ignore all white space characters in the $regex pattern ("r e g \n e x" will be "regex")
s Allow the dot character (i.e. .) to match all characters including newline characters.

$regex usage

Consider a collection named articles that holds documents that resemble to the following document:

{
    name: "MongoDB regular expression",
    content: "In this article we will talk about MongoDB regex expression, ...",
    views: 50,
    tags: ["NoSQL", "MongoDB"]
}

Finding the articles containing the specified word

In the following example we will use the $regex oparator to look for all the documents containing $regex word in there content.

db.articles.find({content: {$regex: "regex"}})

Abreviation

db.articles.find({content: /regex/})

Finding the documents starting with the specified word

To match the start of a string we use the "^" keyword.

db.articles.find({content: /^In this article/})

Finding the documents ending with the specified word

To match the end of a string we use the "$" keyword.

db.articles.find({content: /regex expression, ...$/})

Finding the documents starting and ending with some specified word

To match any word inside a string we use the "." keyword and to match an undefined number of it we use ".*".

db.articles.find({content: /^In this article.*regex expression, ...$/})

Using $regex with Insensitive Case

To do so we add the "i" option to the regex expression.

db.articles.find({content: /^in this article.*regex expression, ...$/i})

Using $regex with Array

In this example we will find all the documents containing a tag that contains java.

db.articles.find({tags: {$regex: "mongo", options: "i"}})