$unset Operator in MongoDB: Quick Guide

Unset Operator Featured Image

In this guide, I will cover the $unset operator in MongoDB inside the mongo shell.

The $unset operator is used to remove a field from documents. this tutorial will walk you through a quick and easy guide to use the $unset operator inside the mongo shell.

To read the Nodejs driver tutorial with examples on using the $unset operator with the Mongoose ODM, click the post below.

Related Post: Remove a Field Completely from a MongoDB Document

This guide will demonstrate with examples, how to use the unset operator to remove a field in MongoDB.

The $unset Operator Syntax (Mongo Shell)

Let us look at the mongo shell syntax of the $unset operator in MongoDB:

{ $unset: { field1: "", ... } }

Using the $unset Operator inside the Mongo Shell

Let us start with the examples to learn to use the $unset operator in MongoDB inside the Mongo shell.

Using the $unset Operator on Fields in MongoDB

In this example, I will use the $unset operator to remove a field inside our documents.

  • Start the mongo local database server
  • Open the mongo shell and navigate into a database using the use db_name command
  • Let us take a look at our documents available:
> db.shoes.find({}).pretty()
{
        "_id" : ObjectId("616b18a556e71503e4a0334d"),
        "name" : "Mariana Grider Boots",
        "price" : 23000,
        "size" : 7,
        "style" : "Slingbacks",
        "onSale" : false,
        "freeDelivery" : true,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 40,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("616b18a556e71503e4a0334e"),
        "name" : "Purple Shimmery Heels",
        "price" : 13380,
        "size" : 6,
        "style" : "Peep Toes",
        "onSale" : true,
        "freeDelivery" : false,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        },
        "itemNumber" : 0
}
{
        "_id" : ObjectId("616b18a556e71503e4a0334f"),
        "name" : "Zac Porter Straps",
        "price" : 19760,
        "size" : 8,
        "style" : "Ankle Strap",
        "onSale" : true,
        "freeDelivery" : true,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("616b18a556e71503e4a03350"),
        "name" : "Valerina's Red Carpet Toes",
        "price" : 26000,
        "size" : 9,
        "style" : "Scarpin",
        "onSale" : false,
        "freeDelivery" : false,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("616b18a556e71503e4a03351"),
        "name" : "Neeta Parmar's Stylish Bees 'n' Honey Boots",
        "price" : 10780,
        "size" : 7,
        "style" : "Wedge Booties",
        "onSale" : true,
        "freeDelivery" : true,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("6171bbf968378cae00d83aca"),
        "name" : "Caroline Wielder Shoes",
        "price" : NumberDecimal("14999.985"),
        "size" : 5,
        "style" : "Wedge Booties",
        "onSale" : true,
        "freeDelivery" : false,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("6171bbf968378cae00d83acb"),
        "name" : "Marilyn Jaworski Sky Highs",
        "price" : NumberDecimal("12399.85"),
        "size" : 5,
        "style" : "Scarpin",
        "onSale" : false,
        "freeDelivery" : false,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
  • Let’s say we want to remove the freeDelivery field from all our documents. We will use the $unset operator to help us out:
> db.shoes.updateMany(
...    { },
...    { $unset: { "freeDelivery" : "" } }
... )

{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }
  • Let us have a look at our documents:
> db.shoes.find({}).pretty()

{
        "_id" : ObjectId("616b18a556e71503e4a0334d"),
        "name" : "Mariana Grider Boots",
        "price" : 23000,
        "size" : 7,
        "style" : "Slingbacks",
        "onSale" : false,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 40,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("616b18a556e71503e4a0334e"),
        "name" : "Purple Shimmery Heels",
        "price" : 13380,
        "size" : 6,
        "style" : "Peep Toes",
        "onSale" : true,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        },
        "itemNumber" : 0
}
{
        "_id" : ObjectId("616b18a556e71503e4a0334f"),
        "name" : "Zac Porter Straps",
        "price" : 19760,
        "size" : 8,
        "style" : "Ankle Strap",
        "onSale" : true,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("616b18a556e71503e4a03350"),
        "name" : "Valerina's Red Carpet Toes",
        "price" : 26000,
        "size" : 9,
        "style" : "Scarpin",
        "onSale" : false,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("616b18a556e71503e4a03351"),
        "name" : "Neeta Parmar's Stylish Bees 'n' Honey Boots",
        "price" : 10780,
        "size" : 7,
        "style" : "Wedge Booties",
        "onSale" : true,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("6171bbf968378cae00d83aca"),
        "name" : "Caroline Wielder Shoes",
        "price" : NumberDecimal("14999.985"),
        "size" : 5,
        "style" : "Wedge Booties",
        "onSale" : true,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("6171bbf968378cae00d83acb"),
        "name" : "Marilyn Jaworski Sky Highs",
        "price" : NumberDecimal("12399.85"),
        "size" : 5,
        "style" : "Scarpin",
        "onSale" : false,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}

We have successfully deleted or removed the freeDelivery field from all our documents using the $unset operator in MongoDB.

Using the $unset Operator on Nested Documents in MongoDB

We will now remove an embedded field or document from our documents using the $unset operator.

> db.shoes.updateMany(
...    { },
...    { $unset: { "moreDetails.colors" : "" } }
... )

{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }
  • Let us take a look at our documents now:
> db.shoes.find({}).pretty()

{
        "_id" : ObjectId("616b18a556e71503e4a0334d"),
        "name" : "Mariana Grider Boots",
        "price" : 23000,
        "size" : 7,
        "style" : "Slingbacks",
        "onSale" : false,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 40
        }
}
{
        "_id" : ObjectId("616b18a556e71503e4a0334e"),
        "name" : "Purple Shimmery Heels",
        "price" : 13380,
        "size" : 6,
        "style" : "Peep Toes",
        "onSale" : true,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10
        },
        "itemNumber" : 0
}
{
        "_id" : ObjectId("616b18a556e71503e4a0334f"),
        "name" : "Zac Porter Straps",
        "price" : 19760,
        "size" : 8,
        "style" : "Ankle Strap",
        "onSale" : true,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10
        }
}
{
        "_id" : ObjectId("616b18a556e71503e4a03350"),
        "name" : "Valerina's Red Carpet Toes",
        "price" : 26000,
        "size" : 9,
        "style" : "Scarpin",
        "onSale" : false,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10
        }
}
{
        "_id" : ObjectId("616b18a556e71503e4a03351"),
        "name" : "Neeta Parmar's Stylish Bees 'n' Honey Boots",
        "price" : 10780,
        "size" : 7,
        "style" : "Wedge Booties",
        "onSale" : true,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10
        }
}
{
        "_id" : ObjectId("6171bbf968378cae00d83aca"),
        "name" : "Caroline Wielder Shoes",
        "price" : NumberDecimal("14999.985"),
        "size" : 5,
        "style" : "Wedge Booties",
        "onSale" : true,
        "moreDetails" : {
                "qtyAvlbl" : 10
        }
}
{
        "_id" : ObjectId("6171bbf968378cae00d83acb"),
        "name" : "Marilyn Jaworski Sky Highs",
        "price" : NumberDecimal("12399.85"),
        "size" : 5,
        "style" : "Scarpin",
        "onSale" : false,
        "moreDetails" : {
                "qtyAvlbl" : 10
        }
}

We have successfully removed the colors nested document from all our documents.

Using the $unset Operator on Missing or Non-Existent Fields

Let us try removing a field that does not exist in any of our documents:

> db.shoes.updateMany(
...    { },
...    { $unset: { "isAvailable" : "" } }
... )

{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 0 }

The modifiedCount property shows a 0 value which indicates the operation was no-op meaning, it did not change anything.

Conclusion

In this article, we learned how to use the $unset Operator in MongoDB with multiple examples. If you are interested in more such tutorials, keep a tab on the latest releases at CodeForGeek.com

Noteworthy References

MongoDB Docs