MongoDB

MongoDB Regular Expressions Example

Hello readers, a regular expression or regex is a sequence of characters that explains a specific search pattern. In this tutorial, we will learn how to use the regular expressions in the Mongo database.

1. Introduction

If you have installed the MongoDB application (version 3.6) on Windows or Ubuntu operating system and you wish to learn the regular expressions then follow the below steps. It is very simple, but before moving further let’s take a look at the Mongo database and its characteristics.

1.1 What is MongoDB?

  • MongoDB is a high-performance NoSQL database where each database has collections which in turn has documents. Each document has a different number of fields, size, content, and is stored in a JSON-like format (i.e. Binary JSON (BSN))
  • The documents in MongoDB doesn’t need to have a schema defined beforehand. Instead, the fields (i.e. records) can be created on the go
  • Data model available within the MongoDB allows developers to represent the hierarchical relationships, store arrays, and other more complex structures easily
  • This NoSQL solution often comes with embedding, auto-sharding, and onboard replication for better scalability and high availability

1.1.1 Why MongoDB?

  • As a NoSQL type database, MongoDB stores the data in the form of a document. Thus, MongoDB offers more flexibility
  • This database supports search by field-name, range queries, and the regular expressions. It often provides queries to return the particular fields inside the documents
  • MongoDB offers indexes to improve the search performance within the NoSQL database
  • To offer horizontal scalability, MongoDB uses sharding by splitting the data across the many MongoDB occurrences
  • Replication: MongoDB can give high availability with the replica sets

1.2 What is a Cursor in MongoDB?

In Mongo world, a cursor is an object that allows developers to iterate through the documents of a Mongo collection. The behavior of cursor allows an automatic iteration across the results of the query; however, developers can explicitly go through the items returned in the cursor object. The below diagram lists 4 documents where the Mongo cursor will point to the first document and then iterate through all the other documents of a collection.

Fig. 1: Pictorial representation of a Cursor in Mongo collection
Fig. 1: Pictorial representation of a Cursor in Mongo collection

1.2.1 Why Cursor in MongoDB?

Cursor offers:

  • A true snapshot of a system i.e. it returns the data in batches and increases the database performance
  • It saves system memory by allowing batch inserts and updates
  • Intelligibility and Clarity on the ad-hoc and complex queries of the sequential nature having large result sets and low consistency requirements
  • Openness to work on small batches of data as developers don’t need to wait for the processing and download of the complete record-set

2. MongoDB Regular Expression Example

In this tutorial, we will learn how to handle the “regular expressions” in the Mongo database.

2.1 $regex method in the Mongo database

In the Mongo universe, the $regex operator allows the user to get those documents from a collection where a string matches a specified pattern. The $regex operator has the following prototype form:

Mongo database ‘$regex’ Operator Syntax

> db.collection_name.find( { <field_name>: { $regex: /pattern/, $options: '<options>' } } )

Where:

  • The field_name in the find() method is an attribute on which the documents are fetched from a Mongo collection
  • A pattern is a regular expression for a complex search

Below table lists the different <options> attribute that is available in the Mongo database.

OptionDescriptionSyntax Restrictions
iAllows the developers to match the upper and the lower case characters.
mAllows the developers to match the patterns that include the anchor tags (i.e. ^ for the start and $ for the end).
xAllows the developers to ignore all the white space characters in the $regex pattern.Requires $regex with $options syntax
sAllows the dot character (i.e. .) to match all the characters including the newline characterRequires $regex with $options syntax

2.2 Practical usage

Let’s understand the implementation of this method with the help of the sample snippets.

2.2.1 Start MongoDB

Start a standalone mongod instance as shown below.

Fig. 2: Start Mongo instance
Fig. 2: Start Mongo instance

2.2.2 Connect to the Mongo Instance

Connect with the mongo shell to make a connection with the MongoDB instance on the port 27017 as shown below.

Fig. 3: Connect to Mongo database
Fig. 3: Connect to Mongo database

2.2.3 Create Mongo database and collection

To begin with the implementation, we will need to create a sample database and collection. The below script creates a database called bakery with a collection of products. Open the Mongo terminal and execute the script.

Database & Collection creation script

> use bakery

> db.products.insertMany( [
	{ _id: 1, item: { category: "cake", type: "chiffon" }, amount: 10 },
	{ _id: 2, item: { category: "cookies", type: "chocolate chip" }, amount: 50 },
	{ _id: 3, item: { category: "cookies", type: "chocolate chip" }, amount: 15 },
	{ _id: 4, item: { category: "cake", type: "lemon" }, amount: 30 },
	{ _id: 5, item: { category: "cake", type: "carrot" }, amount: 20 },
	{ _id: 6, item: { category: "brownies", type: "blondie" }, amount: 10 }
] )

The script gives the below output.

Fig. 4: Database & Collection creation
Fig. 4: Database & Collection creation

2.2.4 Check Mongo database and collection

If the script works well, the database and the collection will be shown in the Mongo Workbench. Using the db.collection_name.find() command the documents of a collection will be shown as below.

Fig. 5: Mongo database & collection
Fig. 5: Mongo database & collection

2.2.5 Implementation of Regular Expressions

Now, go back to the Mongo shell and let’s understand the practical implementation of ‘regex’ expression in the Mongo world. The following Mongo database command can be used.

Query 1

> db.products.find( { "item.category" :{ $regex: "cookies" } } ).pretty()

Alternatively, developers can re-write the Query 1 regular expression in the following way i.e.

Query 1(a)

> db.products.find( { "item.category" :{ $regex: /cookies/ } } ).pretty()

As shown in Fig. 6, the Query 1 and Query 1(a) snippet will return those documents from the products collection that matches the given regex pattern.

Fig. 6: Mongo database '$regex' Operator
Fig. 6: Mongo database ‘$regex’ Operator

Let’s say developers want to use a regular expression with case-insensitive string. They can do this by simply using the $options parameter with its value as $i. The following Mongo database command can be used.

Query 2

> db.products.find( { "item.category" :{ $regex: "CAKE", $options: "$i"} } ).pretty()

As shown in Fig. 7, the Query 2 snippet will return those documents from the products collection that has the word CAKE in different cases.

Fig. 7: Mongo database '$regex' Operator with Options
Fig. 7: Mongo database ‘$regex’ Operator with Options

In MongoDB, regex or regular expressions can also be used on the array fields. This approach is important when developers are using the tags functionality. Let’s understand this with the help of a sample query snippet.

Query 3

> db.products_tags.insertMany( [
	{ _id: 1, post_text: "Enjoy the cake!", "tags": [ "cake", "bakery" ] },
	{ _id: 2, post_text: "Enjoy the cookies!", "tags": [ "bakery", "cookie" ] },
	{ _id: 3, post_text: "Enjoy the pastry!", "tags": [ "pastry", "cake" ] }
] )

> db.products_tags.find( { tags:{ $regex: "bake" } } ).pretty()

As shown in Fig. 8, the Query 3 snippet will return those documents from the products_tags collection that have tags beginning from the word bake.

Fig. 8: Mongo database '$regex' Operator for Array elements
Fig. 8: Mongo database ‘$regex’ Operator for Array elements

2.3 Regular Expression Optimization

Developers can optimize the regular expression queries in the MongoDB by following the query optimization concepts i.e.

  • If the fields of the Mongo documents are indexed, the search operation on the indexed fields will be faster as compared to scanning the whole collection
  • If the regular expression is a prefix expression, then all the matches must start with certain string characters

That’s all for this post. Happy Learning!!

3. Conclusion

In this tutorial, we learned about the use of regular expressions in the Mongo database.

  • Patten matching in MongoDB is achieved by the $regex operator
  • The ^ and $ symbol are used for exact text searches, where ^ and $ make sure that a string starts and end with a specific character
  • The delimiters (i.e. //) can also be used for the pattern matching in MongoDB
  • The option $i specifies the case-insensitive strings

Developers can download the sample commands in the Downloads section.

4. Download the Eclipse Project

This was an example of implementing and using the regular expressions in the Mongo database.

Download
You can download the full source code of this example here: CodeSnippet

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button