$pop Operator in MongoDB: Basic Guide

You may heard about the JavaScript pop() method used to pop or remove elements from an array. Similarly, MongoDB has a $pop operator that provides similar functionality of removing an element from an array. 

But, there is a catch here, you can not just simply use this operator to remove any element from an array, there are some criteria that we will see in detail in this tutorial. Let’s get started.

MongoDB $pop Operator

MongoDB $pop operator removes an element from an array but the element can be only the first or last element. 

For removing the first element from an array, it is required to pass the array field with a value of -1 and for removing the last element the value should be 1. Also, the $pop fails if the specified field is not an array field. 

After removing the last element, if there are no elements left, Mongo may remove the array field from the document.

Syntax:

db.myCollection.update({ }, { $pop: { field: -1 | 1}});

Parameters:

  1. “collection” is the collection name where we are looking for the documents.
  2. “update()” is the method we are using to update the documents.
  3. In “{ field: -1 | 1}”, the field represents an array field within the documents, and -1 or 1 specifies whether to remove the first or last element from that array field.

Note: Here we have used the update() method with $pop but you can use any update operation method, such as findAndUpdate(), updateMany(), etc.

Removing Elements from Array Fields Using the $pop Operator

In this section, you will see some practical examples of removing the first and last element from an array field using the $pop operator.

Setup:

1. Start the MongoDB shell.

2. Execute the following commands to list all the available databases & move into one of them.

show dbs
use dronesDen

Here we are using the “droneDen” database where we have already created a collection “drones” having documents containing an array field so that we can demonstrate removing elements from that.

New to MongoDB? Explore our guide on how to list users, collections, and databases in the Mongo Shell.

3. Execute the below query to display documents from a collection.

> db.drones.find({}).pretty()
{
        "_id" : ObjectId("615f1672a7c6532312e3a41d"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Aurelize Frinworks Hawk 7",
        "price" : 12000,
        "weight" : "34 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Security",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f16eca7c6532312e3a41e"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Poka Moonskull 2X67",
        "price" : 76400,
        "weight" : "48 kilograms",
        "additionalDetails" : {
                "material" : "nylon",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jade Balestrom RW",
        "price" : 24500,
        "weight" : "27 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("615f1879a7c6532312e3a420"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "VarX Lourra VQ (Limited Edition)",
        "price" : 49500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615f1922a7c6532312e3a421"),
        "utility" : [
                "Security",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Mellori Styxe 175",
        "price" : 15000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}

Example 1: Remove the First Element from an Array Field

As we already mentioned, to remove the first item from an array field using the $pop operator we have to pass the value of -1 for the array field.

Below is the query to remove the first item from an array field:

> db.drones.updateOne({name: "Mellori Styxe 175"}, {$pop: { utility: -1}})

{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

Here we are removing the first element from the “utility” array from the drone whose name field is “Mallory Styx 175”.

Let’s see what our documents look like after the update operation:

> db.drones.find({}).pretty()

{
        "_id" : ObjectId("615f1672a7c6532312e3a41d"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Aurelize Frinworks Hawk 7",
        "price" : 12000,
        "weight" : "34 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Security",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f16eca7c6532312e3a41e"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Poka Moonskull 2X67",
        "price" : 76400,
        "weight" : "48 kilograms",
        "additionalDetails" : {
                "material" : "nylon",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jade Balestrom RW",
        "price" : 24500,
        "weight" : "27 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("615f1879a7c6532312e3a420"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "VarX Lourra VQ (Limited Edition)",
        "price" : 49500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615f1922a7c6532312e3a421"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Mellori Styxe 175",
        "price" : 15000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}

The first element from the “utility” array of the “Mellori Styxe 175” drone is now removed.

Example 2: Remove the Last Element from an Array Field

To remove the last item from an array field using the $pop operator we have to pass the value of 1 for the array field.

Below is the query to remove the last item from an array field:

> db.drones.updateOne({name: "VarX Lourra VQ (Limited Edition)"}, {$pop: { utility: 1}})

{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

Here we are removing the last element from the “utility” array from the drone whose name field is “VarX Lourra VQ (Limited Edition)”.

Let’s see what our documents look like after the update operation:

> db.drones.find({}).pretty()
{
        "_id" : ObjectId("615f1672a7c6532312e3a41d"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Aurelize Frinworks Hawk 7",
        "price" : 12000,
        "weight" : "34 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Security",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f16eca7c6532312e3a41e"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Poka Moonskull 2X67",
        "price" : 76400,
        "weight" : "48 kilograms",
        "additionalDetails" : {
                "material" : "nylon",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jade Balestrom RW",
        "price" : 24500,
        "weight" : "27 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("615f1879a7c6532312e3a420"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue"
        ],
        "onSale" : false,
        "name" : "VarX Lourra VQ (Limited Edition)",
        "price" : 49500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615f1922a7c6532312e3a421"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Mellori Styxe 175",
        "price" : 15000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}

We have successfully removed the last element from the “utility” field of the “VarX Lourra VQ (Limited Edition)” drone using the $pop operator.

Example 3: Remove the First Element from the Nested Array Fields

Nested array fields mean a field containing an array is a value of another field inside the document. In other words, it does not exist in the top level of the document, it is contained inside another field. 

In our document, the “moreUses” is a nested array field contained inside the “additionalDetails” field.

Below is the query to remove the first element from it using dot notation:

> db.drones.updateMany({}, {$pop: { "additionalDetails.moreUses": -1}})

{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }

Let’s see what our documents look like now:

> db.drones.find({}).pretty()

{
        "_id" : ObjectId("615f1672a7c6532312e3a41d"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Aurelize Frinworks Hawk 7",
        "price" : 12000,
        "weight" : "34 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f16eca7c6532312e3a41e"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Poka Moonskull 2X67",
        "price" : 76400,
        "weight" : "48 kilograms",
        "additionalDetails" : {
                "material" : "nylon",
                "moreUses" : [
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jade Balestrom RW",
        "price" : 24500,
        "weight" : "27 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("615f1879a7c6532312e3a420"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue"
        ],
        "onSale" : false,
        "name" : "VarX Lourra VQ (Limited Edition)",
        "price" : 49500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615f1922a7c6532312e3a421"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Mellori Styxe 175",
        "price" : 15000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Recreation"
                ]
        }
}

We have successfully removed the first elements of all the documents’ embedded arrays using the $pop operator.

Read More: $in Operator in MongoDB

Summary

In this article, we have learned about the Mongo update operator called $pop. In the computer science world, pop means to remove something, which is what this operator does. $pop is used to remove the first or last element of the array. We have explained the syntax of using it and also given easy examples like removing the first item from an array field, the last item from an array field and the first item from a nested array field.

The working of the $pop is very simple, it is handy in places where we only have to remove the first or last element from an array. We would recommend that you try it on your own MongoDB database to understand its use case even better. We hope you have got answers to all your questions.

Reference

$pop — MongoDB Manual

Aneesha S
Aneesha S
Articles: 171