$each Operator in MongoDB: Comprehensive Guide

Each Operator Featured Image

In this tutorial, I am going to cover the $each operator in MongoDB.

There are a variety of operators in MongoDB, so you can choose from them to satisfy a variety of needs. Through its very powerful, easy-to-use, and flexible operators, the MongoDB database provides you the ability to build applications that can perform far more than CRUD operations. Its popularity comes as no surprise; MongoDB provides a great deal of power to applications.

In addition to handling queries, printing, and updating data, MongoDB provides a wider set of operators. With its easy-to-use interface, it has something for everyone, regardless of its users’ experience level.

I’ll discuss the $each operator today, one of MongoDB’s most useful and commonly used operators.

The $each operator has brought a lot of convenience with its introduction into MongoDB. This operator works on arrays and can be used with the $addToSet operator and the $push operator, examples of which I will cover in this guide.

The $each operator when used with the $addToSet operator, allows us to add multiple elements to an array if they don’t exist. When it is used with the $push operator, we can simply append or insert elements in an array.

The $each Operator Syntax

Let us take a look at the $each operator syntaxes.

For $addToSet:

{ $addToSet: { : { $each: [ ,  ... ] } } }

For $push

{ $push: { : { $each: [ ,  ... ] } } }

Using the $each Operator with $addToSet

Let us start by using the $each operator with the $addToSet operator to add elements in an array field that don’t exist.

  • Start up your MongoDB server
  • Decide on which database you want to use and move into it:
show dbs
use droneShop
  • Taking a quick look at all our documents in the drones collection:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("61573946341d653bccaf7827"),
        "utility" : [
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Thunderbird Phoenix ",
        "price" : 2000,
        "weight" : "1.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615739bb341d653bccaf7828"),
        "utility" : [
                "Delivery",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "FR - Hummingbird",
        "price" : 2000,
        "weight" : "20 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f092d0a441e040c3fdc"),
        "utility" : [
                "Photography",
                "Monitoring or Inspection",
                "Security",
                "Recreation"
        ],
        "onSale" : false,
        "name" : "Dixy Magenta",
        "price" : 2000,
        "weight" : "4.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f6e2d0a441e040c3fdd"),
        "utility" : [
                "Photography",
                "Monitoring or Inspection",
                "Security",
                "Recreation"
        ],
        "onSale" : false,
        "name" : "UJ - Spear Eye Forest Green",
        "price" : 2000,
        "weight" : "3 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61598fe5ebf45d32b85bd1a3"),
        "utility" : [
                "Security",
                "Monitoring or Inspection",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Golden Pegasus 57B",
        "price" : 2000,
        "weight" : "5 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61599147ebf45d32b85bd1a4"),
        "utility" : [
                "Security",
                "Monitoring or Inspection",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "P12 - Mercenary SF",
        "price" : 2000,
        "weight" : "2 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615991e9ebf45d32b85bd1a6"),
        "utility" : [
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Meteor Rada W54",
        "price" : 2000,
        "weight" : "7 kilograms",
        "__v" : 0
}
  • Let us now add a few elements into the “utility” array field:
> db.drones.updateMany( {}, { $addToSet: { utility: { $each: [ "Videography", "Combat", "Rescue", "Construction" ] } } } )

{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }
  • Printing our documents to see how they look:
> db.drones.find({}).pretty()
{
        "_id" : ObjectId("61573946341d653bccaf7827"),
        "utility" : [
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Thunderbird Phoenix ",
        "price" : 2000,
        "weight" : "1.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615739bb341d653bccaf7828"),
        "utility" : [
                "Delivery",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "FR - Hummingbird",
        "price" : 2000,
        "weight" : "20 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f092d0a441e040c3fdc"),
        "utility" : [
                "Photography",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Dixy Magenta",
        "price" : 2000,
        "weight" : "4.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f6e2d0a441e040c3fdd"),
        "utility" : [
                "Photography",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "UJ - Spear Eye Forest Green",
        "price" : 2000,
        "weight" : "3 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61598fe5ebf45d32b85bd1a3"),
        "utility" : [
                "Security",
                "Monitoring or Inspection",
                "Recreation",
                "Photography",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Golden Pegasus 57B",
        "price" : 2000,
        "weight" : "5 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61599147ebf45d32b85bd1a4"),
        "utility" : [
                "Security",
                "Monitoring or Inspection",
                "Recreation",
                "Photography",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "P12 - Mercenary SF",
        "price" : 2000,
        "weight" : "2 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615991e9ebf45d32b85bd1a6"),
        "utility" : [
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Meteor Rada W54",
        "price" : 2000,
        "weight" : "7 kilograms",
        "__v" : 0
}

Using the $each Operator with $push

Let us use the $each operator in MongoDB with the $push operator.

> db.drones.update( { name: `Golden Pegasus 57B` }, { $push: { utility: { $each: [ `Bulky`, `Heavy`, `Lightweight` ] } } } )

{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
  • Checking the document, we just updated:
> db.drones.find( { name: `Golden Pegasus 57B` } ).pretty()

{
        "_id" : ObjectId("61598fe5ebf45d32b85bd1a3"),
        "utility" : [
                "Security",
                "Monitoring or Inspection",
                "Recreation",
                "Photography",
                "Videography",
                "Combat",
                "Rescue",
                "Construction",
                "Bulky",
                "Heavy",
                "Lightweight"
        ],
        "onSale" : false,
        "name" : "Golden Pegasus 57B",
        "price" : 2000,
        "weight" : "5 kilograms",
        "__v" : 0
}

This way we have successfully learned to use the $each operator with the $addToSet operator and the $push operator in MongoDB.

Read More: addToSet Operator with Mongoose, Push Operator

Conclusion

Learn to use the $each operator to insert and append elements to array fields by using it with the $addToSet operator and the $push operator in MongoDB.

Noteworthy References

MongoDB Docs