$eq Operator in MongoDB: Comprehensive 2021 Guide

In this tutorial, I am going to cover the $eq operator also known as the equality operator in MongoDB.

MongoDB allows you to build applications that do much more than CRUD functions. Using MongoDB, you can choose from a wide range of operators and features. A key reason for MongoDB’s popularity is its power and flexibility.

It allows querying, printing, and altering data as well as providing a variety of operators. The benefits of MongoDB can still be appreciated regardless of how experienced their users are.

Today, I am conquering the $eq operator or the equality operator in MongoDB. The $eq operator helps in matching the field values with the given or specified values. This operator works with every type of data for example array fields, object fields (nested documents), and string and number value fields.

This guide aims to provide you with practical examples so you can understand how to use the $eq operator in MongoDB for your applications and projects. So, let’s start.

The $eq Operator Syntax in MongoDB

Let us give a glance at the $eq operator syntax in MongoDB:

{field: {$eq: value}}

Using the $eq Operator in MongoDB

Let us quickly start with the many examples that I want to demonstrate using the $eq operator.

Finding Documents with Numbers as Field Values Using the $eq Operator

In this example, I will use the $eq operator to search and output all those all documents inside my collection that contain a specified numerical value.

  • Start up the MongoDB service
  • Choose the database you wish to work in and navigate into it:
show dbs
use dronesVerse
  • Here’s what the collection looks like before we start:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("615f1672a7c6532312e3a41d"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Taze Sharp Honeyeye G7",
        "price" : 17000,
        "weight" : "9.8 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Security",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f16eca7c6532312e3a41e"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jester Chimpskull 1X85",
        "price" : 23400,
        "weight" : "11 kilograms",
        "additionalDetails" : {
                "material" : "nylon",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Emerald Robin 78 (Limited Edition)",
        "price" : 33500,
        "weight" : "17 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("615f1879a7c6532312e3a420"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "GarX Jerrom BQ (Limited Edition)",
        "price" : 39500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615f1922a7c6532312e3a421"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Wenderman’s X-copter B32",
        "price" : 19500,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
  • Now, we want to look for a drone that costs 39500. This is where we need the $eq operator:
> db.drones.find({price: {$eq: 39500}}).pretty()
{
        "_id" : ObjectId("615f1879a7c6532312e3a420"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "GarX Jerrom BQ (Limited Edition)",
        "price" : 39500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}

Fabulous! Here’s our drone found by the $eq operator which has a price of 39500.

Finding Nested Documents with Strings as Field Values Using the $eq Operator

In this example, I will use the $eq operator to search and output all those nested documents inside my collection that contain a specified string value. I will use dot notation here.

> db.drones.find({"additionalDetails.material": {$eq: "lithium"}}).pretty()

{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Emerald Robin 78 (Limited Edition)",
        "price" : 33500,
        "weight" : "17 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}

Perfect! We have successfully found a nested document with the specified string value with the help of the $eq operator in MongoDB.

Finding Array Fields with String Values Using the $eq Operator

In this example, I am going to look for those documents whose array fields match a specific string value with the $eq operator.

> db.drones.find({utility: {$eq: "Videography"}}).pretty()
{
        "_id" : ObjectId("615f1672a7c6532312e3a41d"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Taze Sharp Honeyeye G7",
        "price" : 17000,
        "weight" : "9.8 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Security",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f16eca7c6532312e3a41e"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jester Chimpskull 1X85",
        "price" : 23400,
        "weight" : "11 kilograms",
        "additionalDetails" : {
                "material" : "nylon",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Emerald Robin 78 (Limited Edition)",
        "price" : 33500,
        "weight" : "17 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("615f1879a7c6532312e3a420"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "GarX Jerrom BQ (Limited Edition)",
        "price" : 39500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615f1922a7c6532312e3a421"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Wenderman’s X-copter B32",
        "price" : 19500,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}

Basically, all our values in the “utility” array field are the same for all documents that’s why I see all the documents printed here.

Matching Array Fields with an Array Using the $eq Operator

In this example, I am going to match those documents using the $eq operator whose array field values match with a given array.

> db.drones.find({"additionalDetails.moreUses": {$eq: [ "Monitoring or Inspection", "Recreation" ]}}).pretty()
{
        "_id" : ObjectId("615f16eca7c6532312e3a41e"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jester Chimpskull 1X85",
        "price" : 23400,
        "weight" : "11 kilograms",
        "additionalDetails" : {
                "material" : "nylon",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f1922a7c6532312e3a421"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Wenderman’s X-copter B32",
        "price" : 19500,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}

Great! We have successfully matched a few documents with a specified array with the $eq operator in MongoDB.

Read More: $ne Operator in MongoDB

Conclusion

That brings us to the end of our eq operator tutorial. We hope you enjoyed learning about this operator through this simple tutorial.

Aneesha S
Aneesha S
Articles: 171