$push Operator in MongoDB: Ultimate Beginner’s Guide

$push Operator Featured Image

In this tutorial, I am going to illustrate examples to explain the $push operator in MongoDB.

Also read: $pull Operator in MongoDB: Quick Guide

In MongoDB, you can pick from a variety of operators that can be used to serve a range of purposes. With MongoDB, you can develop applications that do much more than just CRUD, thanks to its easy-to-use, extremely sophisticated, and flexible operators. This is certainly another factor contributing to MongoDB’s popularity.

Through an expanded set of operators, MongoDB now handles queries, printing, and updating tasks. There is something for everyone with its easy-to-use interface, regardless of experience level.

As one of the most commonly used and quite useful operators provided by MongoDB, today I’m going to discuss its $push operator.

The $push operator is used when you want to append or insert a value to an array. In case the given field is not present in the document, then this operator will simply create that array field and append the value as its element.

It is important to note that you can only provide an array field to the $push operator, otherwise the operation will fail. If the given value is an array, then, it will be appended as a single element into the given array field. If you want to insert each value separately, you may use the $each operator with the $push operator.

This beginner’s guide will demonstrate examples so you can learn to use the $push operator in your MongoDB-backed applications smoothly.

Let’s get going.

The $push Operator Syntax in MongoDB

Let us take a look at the $push operator syntax:

{ $push: { : , ... } }

The $push operator in MongoDB can be used with the findAndUpdate(), update(), and other update operation methods.

Inserting a Single Element to an Array with $push Operator

Let us start to use the $push operator to insert a single element to an array field in our sample database.

  • Start your MongoDB service
  • Choose the database you want to use and move into it:
show dbs
use droneStore
  • Let us see all our documents in our drones collection:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("61573946341d653bccaf7827"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "Thunderbird Phoenix ",
        "price" : 6300,
        "weight" : "1.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615739bb341d653bccaf7828"),
        "utility" : [
                "Delivery"
        ],
        "onSale" : false,
        "name" : "FR - Hummingbird",
        "price" : 45000,
        "weight" : "20 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f092d0a441e040c3fdc"),
        "utility" : [
                "Photography"
        ],
        "onSale" : false,
        "name" : "Dixy Magenta",
        "price" : 4000,
        "weight" : "4.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f6e2d0a441e040c3fdd"),
        "utility" : [
                "Photography"
        ],
        "onSale" : false,
        "name" : "UJ - Spear Eye Forest Green",
        "price" : 10000,
        "weight" : "3 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61598fe5ebf45d32b85bd1a3"),
        "utility" : [
                "Security"
        ],
        "onSale" : false,
        "name" : "Golden Pegasus 57B",
        "price" : 75000,
        "weight" : "5 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61599147ebf45d32b85bd1a4"),
        "utility" : [
                "Security"
        ],
        "onSale" : false,
        "name" : "P12 - Mercenary SF",
        "price" : 12000,
        "weight" : "2 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615991e9ebf45d32b85bd1a6"),
        "utility" : [
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "Meteor Rada W54",
        "price" : 6000,
        "weight" : "7 kilograms",
        "__v" : 0
}
  • Now, let’s say we want to insert “Monitoring or Inspection” into the utility array field, for all the documents. Let’s see how we can do that using the $push operator:
> db.drones.updateMany( {}, { $push: { utility: `Monitoring or Inspection` } } )

{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }
  • Checking what our documents now look like:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("61573946341d653bccaf7827"),
        "utility" : [
                "Monitoring or Inspection",
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "Thunderbird Phoenix ",
        "price" : 6300,
        "weight" : "1.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615739bb341d653bccaf7828"),
        "utility" : [
                "Delivery",
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "FR - Hummingbird",
        "price" : 45000,
        "weight" : "20 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f092d0a441e040c3fdc"),
        "utility" : [
                "Photography",
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "Dixy Magenta",
        "price" : 4000,
        "weight" : "4.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f6e2d0a441e040c3fdd"),
        "utility" : [
                "Photography",
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "UJ - Spear Eye Forest Green",
        "price" : 10000,
        "weight" : "3 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61598fe5ebf45d32b85bd1a3"),
        "utility" : [
                "Security",
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "Golden Pegasus 57B",
        "price" : 75000,
        "weight" : "5 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61599147ebf45d32b85bd1a4"),
        "utility" : [
                "Security",
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "P12 - Mercenary SF",
        "price" : 12000,
        "weight" : "2 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615991e9ebf45d32b85bd1a6"),
        "utility" : [
                "Monitoring or Inspection",
                "Monitoring or Inspection"
        ],
        "onSale" : false,
        "name" : "Meteor Rada W54",
        "price" : 6000,
        "weight" : "7 kilograms",
        "__v" : 0
}

With this, we have successfully inserted a single value to an array field with the help of the $push operator.

Inserting Multiple Elements to an Array with $push Operator

Let us learn how we can append multiple items to an array using the $push operator.

> db.drones.updateMany( {}, { $push: { utility: { $each: [ `Security`, `Recreation`, `Photography` ] } } } )

{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }
  • Printing all our documents:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("61573946341d653bccaf7827"),
        "utility" : [
                "Monitoring or Inspection",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Thunderbird Phoenix ",
        "price" : 6300,
        "weight" : "1.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615739bb341d653bccaf7828"),
        "utility" : [
                "Delivery",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "FR - Hummingbird",
        "price" : 45000,
        "weight" : "20 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f092d0a441e040c3fdc"),
        "utility" : [
                "Photography",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Dixy Magenta",
        "price" : 4000,
        "weight" : "4.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f6e2d0a441e040c3fdd"),
        "utility" : [
                "Photography",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "UJ - Spear Eye Forest Green",
        "price" : 10000,
        "weight" : "3 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61598fe5ebf45d32b85bd1a3"),
        "utility" : [
                "Security",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Golden Pegasus 57B",
        "price" : 75000,
        "weight" : "5 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61599147ebf45d32b85bd1a4"),
        "utility" : [
                "Security",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "P12 - Mercenary SF",
        "price" : 12000,
        "weight" : "2 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615991e9ebf45d32b85bd1a6"),
        "utility" : [
                "Monitoring or Inspection",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Meteor Rada W54",
        "price" : 6000,
        "weight" : "7 kilograms",
        "__v" : 0
}

With this, we have successfully added multiple items to all our documents using the $push operator.

Read More: Monk vs Mongoose for MongoDB: Instant Comparative Guide of 2021

Conclusion

Learn to use the $push operator to insert values into array fields in the MongoDB database.

Noteworthy References

MongoDB Docs