Use a Regular Expression $Regex in MongoDB
Introduction
“Regular expressions” are utilized for findings strings, typically considered a data type, within documents. All computer programming languages frequently use regular expressions for searching for defined patterns within strings. While retrieving an unidentified field in a document can be frustrating, MongoDB supplies a function for using the regular expression, typically abbreviated as $Regex in MongoDB, to retrieve a document based on its defined pattern or values. This tutorial will explain how to use the regular expressionx000D in MongoDB.
Prerequisite
- MongoDB must be properly installed and configured on the local system.
Using Regex in MongoDB
Begin by examining the documents in the following product
collection:
1 2 3 4 5 6 7 8 9 10 11 12 13 | > db.product.find().pretty() { "_id" : ObjectId("5e0a0e9cc20248e0ea73866c"), "sku" : "D3097731", "name" : "Apple", "description" : "THe future of Mobile" } { "_id" : ObjectId("5e0a0e9cc20248e0ea73866d"), "sku" : "D3097634", "name" : "Samsung", "description" : "The fastest mobile" } |
The above document can be located using the following command:
1 | db.product.find({description:{$regex:"Mobile"}}).pretty(); |
The simplest way to execute this query is:
1 | db.product.find({description:/Mobile/}); |
The above code is designed to look for a string pattern that is composed of the letters Mobile
within the document field description
.
MongoDB regex Expression with Case Sensitive
For case-sensitive situations, the regular expression uses $option and the parameter has a value of $i. This is a situation where the regular expression will work in a case-sensitive situation. Here the query will return the value containing “Junaid”, irrespective of whether upper- or lower-case letters are used.
With an understanding of the basic form of the MongoDB regular expression, the regex expression can be used where case-sensitivity is required. Following is an example with the $option
with a $i
value:
1 | db.product.find({description:{$regex:"The",$options:"$i"}}).pretty(); |
The output should resemble the following:
1 2 3 4 5 6 7 8 9 10 11 12 | { "_id" : ObjectId("5e0a0e9cc20248e0ea73866c"), "sku" : "D3097731", "name" : "Apple", "description" : "THe future of Mobile" } { "_id" : ObjectId("5e0a0e9cc20248e0ea73866d"), "sku" : "D3097634", "name" : "Samsung", "description" : "The fastest mobile" } |
Even though the documents use different letter cases for the word The
, both of the documents were retrieved by MongoDB because the $options
parameter was used. However, if the $options
parameter is removed from the query, then the results should resemble the following:
1 2 3 4 5 6 | { "_id" : ObjectId("5e0a0e9cc20248e0ea73866d"), "sku" : "7654321", "name" : "Samsung", "description" : "The fastest Mobile" } |
Using regex Expression in an Array
The previous section explained how to use a regular expression in a case sensitive query. This section explain how to use a regular expression in an array. This is important whenever the tags
functionality is used in MongoDB. Here a tag can be located by just using a few letters of a word within a collection of documents as shown in the following code:
1 | db.product.find({category:{$regex:"aff"}}); |
First, execute the following code to retrieve the document:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | { "_id" : ObjectId("5e0a0e9cc20248e0ea73866c"), "sku" : "D3097731", "name" : "Apple", "description" : "THe future of Mobile", "category" :[ "fast", "light", "elegant" ] } { "_id" : ObjectId("5e0a0e9cc20248e0ea73866d"), "sku" : "D3097634", "name" : "Samsung", "description" : "The fastest mobile", "category" :[ "fast", "affordable", "elegant" ] } |
The results should resemble the following:
1 2 3 4 5 6 7 8 9 10 11 | { "_id" : ObjectId("5e0a0e9cc20248e0ea73866d"), "sku" : "D3097634", "name" : "Samsung", "description" : "The fastest mobile", "category" :[ "fast", "affordable", "elegant" ] } |
Note that the above query simply looks into the category
field of the product
document for words containing the letters ‘aff’. However, if the pattern is changed to letters that are common to both documents, then both documents will be displayed.
Conclusion
This tutorial explained how to use the regular expressionx000D in MongoDB. The tutorial showed how to examine documents in a product
collection, how to locate a document in a collation and a simplified way of executing the query. The article then explained how to use the MongoDB regex for case-sensitive situations, how to use the $options
parameter and use the regex expression in an array. Remember that when using the $Regex regular expressionx000D in MongoDB, if the pattern is changed to letters that are common to both documents, then both of the documents that have those letters in common will be displayed.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started