Regex in MongoDB: Check if a Field Contains a String

In this tutorial, I am going to talk about regex in MongoDB to help check if a field contains a specified string.

Regex in MongoDB allows us to look up strings in our collections the way we want. It comes with multiple options so we can customize our query to check if a field contains a string. You can literally create your own mini search engine with the help of regex in MongoDB if you do not wish to use Elasticsearch.

In this guide, I will demonstrate examples to let you understand how you can regex in MongoDB to be able to check if a field contains a string or not. So, let’s not waste much time and get going!

Regex in MongoDB Syntax

Let us take a quick look at the syntax of regex in MongoDB:

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

Using Regex in MongoDB

To understand how regex in MongoDB works, we need to take a look at some examples. I will cover up a few different types of examples to support the different ways you can customize your string query with regex in MongoDB.

Finding Documents with a Specific String with Regex in MongoDB

In this example, we will use regex in MongoDB to understand how we can query documents to check if a field contains a string.

  • Run the MongoDB service
  • Choose the database you want to use and move into it:
show dbs
use dronesLand
  • Now, let us first take a quick look at all the available documents in our database:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("615c7c3683a7f705b8ecf0c6"),
        "utility" : [
                "Photography"
        ],
        "onSale" : false,
        "name" : "SV - Mavick Flier 67",
        "price" : 6700,
        "weight" : "2.9 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615c7c7e83a7f705b8ecf0c7"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "RJ - Dextera Arial Eye",
        "price" : 34000,
        "weight" : "3.4 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615c7ce483a7f705b8ecf0c8"),
        "utility" : [
                "Security"
        ],
        "onSale" : false,
        "name" : "SV - Rachel Jones 34",
        "price" : 1900,
        "weight" : "1 kilogram",
        "__v" : 0
}
{
        "_id" : ObjectId("615c7d1b83a7f705b8ecf0c9"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "Drosera Originalis Xtera",
        "price" : 8700,
        "weight" : "2.5 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615c7dd383a7f705b8ecf0ca"),
        "utility" : [
                "Security"
        ],
        "onSale" : false,
        "name" : "FRIN Falcon blaze",
        "price" : 11000,
        "weight" : "3.4 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615c905383a7f705b8ecf0cb"),
        "utility" : [
                "Delivery"
        ],
        "onSale" : false,
        "name" : "SV - Yellow Fin Malcolm",
        "price" : 20000,
        "weight" : "55 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615de1410dcb6e0ff8a90f0c"),
        "utility" : [
                "Photography"
        ],
        "onSale" : false,
        "name" : "FRIN Eagle Blaze",
        "price" : 4500,
        "weight" : "1 kilogram",
        "__v" : 0
}
  • Now, we will check if a field contains a string value with the help of regex in MongoDB:
> db.drones.find({name: {$regex: "SV"}}).pretty()

{
        "_id" : ObjectId("615c7c3683a7f705b8ecf0c6"),
        "utility" : [
                "Photography"
        ],
        "onSale" : false,
        "name" : "SV - Mavick Flier 67",
        "price" : 6700,
        "weight" : "2.9 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615c7ce483a7f705b8ecf0c8"),
        "utility" : [
                "Security"
        ],
        "onSale" : false,
        "name" : "SV - Rachel Jones 34",
        "price" : 1900,
        "weight" : "1 kilogram",
        "__v" : 0
}
{
        "_id" : ObjectId("615c905383a7f705b8ecf0cb"),
        "utility" : [
                "Delivery"
        ],
        "onSale" : false,
        "name" : "SV - Yellow Fin Malcolm",
        "price" : 20000,
        "weight" : "55 kilograms",
        "__v" : 0
}

Perfect! Here, we have successfully found the documents with a specific string value, that is “SV”, with the help of regex in MongoDB.

Finding Documents with Case Insensitive String Values with Regex in MongoDB

In this example, we will use regex in MongoDB to understand how we can query documents to check if a field contains a specific case insensitive string value. We will make use of the $i option with a regular expression in mongo.

The $i option will help us find fields with both lower case and uppercase specified string values. Here’s how we can do that:

> db.drones.find({name:{$regex:"blaze",$options:"$i"}}).pretty()

{
        "_id" : ObjectId("615c7dd383a7f705b8ecf0ca"),
        "utility" : [
                "Security"
        ],
        "onSale" : false,
        "name" : "FRIN Falcon blaze",
        "price" : 11000,
        "weight" : "3.4 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615de1410dcb6e0ff8a90f0c"),
        "utility" : [
                "Photography"
        ],
        "onSale" : false,
        "name" : "FRIN Eagle Blaze",
        "price" : 4500,
        "weight" : "1 kilogram",
        "__v" : 0
}

Amazing, the results are just perfect. Here, we have successfully found documents that contain the string value “blaze”, which is case insensitive, with the help of regex in MongoDB.

Finding Documents with a Specific Starting String Value with Regex in MongoDB

In this example, we will use regex in MongoDB to understand how we can query documents to check if a field starts with a specific string value.

Here’s how we can do it:

> db.drones.find({name:{$regex:"^D"}}).pretty()

{
        "_id" : ObjectId("615c7d1b83a7f705b8ecf0c9"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "Drosera Originalis Xtera",
        "price" : 8700,
        "weight" : "2.5 kilograms",
        "__v" : 0
}

We have successfully found a document that starts with the letter ‘D’ with the help of regex in MongoDB.

Finding Documents with a Specific Ending String Value with Regex in MongoDB

In this example, we will use regex in MongoDB to understand how we can query documents to check if a field ends with a specific string value.

> db.drones.findOne({name:{$regex:"e$"}})

{
        "_id" : ObjectId("615c7c7e83a7f705b8ecf0c7"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "RJ - Dextera Arial Eye",
        "price" : 34000,
        "weight" : "3.4 kilograms",
        "__v" : 0
}

We have successfully found one document that ends with the letter ‘E’ with the help of regex in MongoDB.

Noteworthy References

SO Answer

Aneesha S
Aneesha S
Articles: 172