MongoDB - Regular Expression

MongoDB - Regular Expression

Regular expressions are often used in all languages ​​to find a pattern or word in any string. MongoDB also provides regular expression functionality for string pattern matching using the $ regex operator . MongoDB uses PCRE (Perl Compatible Regular Expression) as its regular expression language.

Unlike text search, we don't need to do any settings or commands to use regular expressions.

Consider the following document structure in a message collection containing the message body and its tags:

{ "post_text" : "enjoy the mongodb articles on tutorialspoint" , "tags" : [ "mongodb" , "tutorialspoint" ] }

Using a regular expression expression

The following regex query searches for all posts containing the string tutorialspoint in it −

> db . posts . find ({ post_text :{ $regex : "tutorialspoint" }})

This same query can also be written as −

> db . posts . find ({ post_text : /tutorialspoint/ })

Using a regular expression expression is case sensitive

To make the search case insensitive, we use the $options parameter with the value $i . The following command will search for lines that have the word tutorialspoint , regardless of lowercase or uppercase −

> db . posts . find ({ post_text :{ $regex : "tutorialspoint" , $options : "$i" }})

One of the results returned by this query is the following document, which contains the word tutorialspoint on different occasions:

{ "_id" : ObjectId ( "53493d37d852429c10000004" ), "post_text" : "hey! this is my post on TutorialsPoint" , "tags" : [ "tutorialspoint" ] }

Using regular expressions for array elements

We can also use the concept of regular expressions on an array field. This is especially important when we implement tag functionality. So, if you want to find all posts that have tags that start with the word tutorial (or tutorial, or tutorial, or tutorialpoint, or tutorialphp), you can use the following code:

If the fields of the document are indexed , the query will use make using the indexed values ​​to match the regular expression. This makes the search very fast compared to a regular expression that scans the entire collection.

If the regular expression is a prefix expression , all matches must start with certain string characters. For example, if the regular expression expression is ^ tut , the query should only look for lines that start with tut .