In this guide, I am going to walk you through the $pop operator in MongoDB. With great features and functionalities, MongoDB is a highly advanced and highly powerful database management system. Beginners often favor MongoDB for its ease of use and powerful capabilities.
While the list of operators is very long, I am going to focus today on the $pop operator in MongoDB. In addition to its popularity among beginners, it has also gained traction among senior developers.
Similar to the JavaScript pop() method, the $pop operator in MongoDB functions to remove the first or the last element in an array. For the $pop operator to remove the first element from the specified array, we must pass -1 as the value. To remove the last element from the specified array, we must pass 1 as the value.
It is important to keep note that if the specified field is not an array field, then the $pop operator will fail. Moreover, when no element remains after removing the last element in the array, then the array will remain empty. The $pop operator can be used with any update operation functions like update(), findAndUpdate(), updateMany() etc.
This guide will provide examples to let you understand how to use the $pop operator in MongoDB. So, let us get started.
The $pop Operator Syntax
{ $pop: { field: -1 | 1, ... } }
Using the $pop Operator in MongoDB
Let us get started with learning to use the $pop operator in MongoDB with several examples.
Removing the First Item from an Array Field Using the $pop Operator
In this example, I will demonstrate an example to explain how to remove the first element from an array field.
- Run the MongoDB service
- Choose from the databases you wish to use and move into it:
show dbs
use dronesDen
- Let us first list out all the available documents in our drones 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"
]
}
}
- Removing the first item or element from an array field using the $pop operator:
> db.drones.updateOne({name: "Mellori Styxe 175"}, {$pop: { utility: -1}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
- Checking what our documents now look like:
> 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 with the $pop operator.
Removing the Last Item from an Array Field Using the $pop Operator
In this example, we shall remove the last element from an array field using the $pop operator in MongoDB.
> db.drones.updateOne({name: "VarX Lourra VQ (Limited Edition)"}, {$pop: { utility: 1}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
- Checking what the array field now looks like:
> 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.
Removing the First Item from Nested Array Fields Using the $pop Operator
In this example, I will demonstrate an example to explain how to remove the first element from an embedded or nested array field for all documents this time. I will use dot notation for querying.
> db.drones.updateMany({}, {$pop: { "additionalDetails.moreUses": -1}})
{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }
- Printing our documents to see what they look like:
> 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: Ultimate 2021 Guide to Using the $in Operator in MongoDB