$ (update) Operator in MongoDB – Quick Guide

In this guide, we will discuss the $ (update) operator in MongoDB.

MongoDB is one of the most popular database management systems accessible today, and it contains a number of sophisticated capabilities. It is never simple for a developer, no matter how brilliant he or she is, to memorize all of its operators and features. In reality, it is much less frequent for a developer to do this than one might think.

A strong database system allows an application to have a wide range of features and capabilities. Furthermore, increased security mechanisms might be put in place. In today’s tutorial, I’ll show you how to deconstruct arrays in MongoDB using the $ (update) operator.

The positional $ operator indicates an element in an array to update despite providing the element’s location in the array.

With MongoDB, you may select from a variety of operators and features. MongoDB enables you to create apps that can do much more than just CRUD operations. As a result, MongoDB is an excellent choice for developing unique apps. Its appeal arises from its adaptability and strength.

In this guide, I will demonstrate with examples how to use the $ (update) operator in MongoDB.

The $ (Update) Operator Syntax in MongoDB

Let us take a quick look at the $ (update) operator syntax:

{ "array.$" : value }

With the update operation:

db.collection.update(
   { array: value ... },
   { update operator: { "array.$" : value } }
)

Using the $ (Update) Operator in MongoDB

Let us take a look at a few examples to understand how to use the $ (update) operator in MongoDB inside the mongo shell with the update operation.

Updating Values in an Array Using $ (Update) Operator in MongoDB

Let us update or modify the value of an array. The positional $ (update) operator simply performs operations on the first match element or field with the updateOne() operation.

Let us start with an example.

  • Start the MongoDB local database server
  • Switch to a database
  • Let us take a look at the documents available for me in my collection:
> db.drones.find().pretty()
{
        “_id” : ObjectId(“61673f46b34f185eb7b2bf0c”),
        “utility” : [
                “Oil, gas, and mineral exploration”,
                “Remote sensing”,
                “Real estate and construction”,
                “Recreation”,
                “Delivery”
        ],
        “onSale” : false,
        “name” : “Nimbari Gryphon Medeta 65”,
        “price” : 77500,
        “weight” : “77 kilograms”,
        “additionalDetails” : {
                “material” : “carbon fiber”,
                “moreUses” : [
                        “Precision Agriculture”,
                        “Land Inspection”,
                        “Water Inspection”,
                        “Cinematography”
                ]
        }
}
{
        “_id” : ObjectId(“61673f46b34f185eb7b2bf0d”),
        “utility” : [
                “Oil, gas, and mineral exploration”,
                “Remote sensing”,
                “Real estate and construction”,
                “Recreation”,
                “Delivery”
        ],
        “onSale” : false,
        “name” : “X-Strimmer Eye”,
        “price” : 23500,
        “weight” : “24 kilograms”,
        “additionalDetails” : {
                “material” : “glass fiber”,
                “moreUses” : [
                        “Precision Agriculture”,
                        “Land Inspection”,
                        “Water Inspection”,
                        “Cinematography”
                ]
        }
}
{
        “_id” : ObjectId(“61673f46b34f185eb7b2bf0e”),
        “utility” : [
                “Oil, gas, and mineral exploration”,
                “Remote sensing”,
                “Real estate and construction”,
                “Recreation”,
                “Delivery”
        ],
        “onSale” : false,
        “name” : “Khai Balemosh Shefqa TRX”,
        “price” : 120500,
        “weight” : “80 kilograms”,
        “additionalDetails” : {
                “material” : “aluminum”,
                “moreUses” : [
                        “Precision Agriculture”,
                        “Land Inspection”,
                        “Water Inspection”,
                        “Cinematography”
                ]
        }
}
{
        “_id” : ObjectId(“61673f46b34f185eb7b2bf0f”),
        “utility” : [
                “Oil, gas, and mineral exploration”,
                “Remote sensing”,
                “Real estate and construction”,
                “Recreation”,
                “Delivery”
        ],
        “onSale” : false,
        “name” : “Sifinist Croma AX”,
        “price” : 99500,
        “weight” : “97 kilograms”,
        “additionalDetails” : {
                “material” : “lithium”,
                “moreUses” : [
                        “Precision Agriculture”,
                        “Land Inspection”,
                        “Water Inspection”,
                        “Cinematography”
                ]
        }
}
{
        “_id” : ObjectId(“61673f46b34f185eb7b2bf10”),
        “utility” : [
                “Oil, gas, and mineral exploration”,
                “Remote sensing”,
                “Real estate and construction”,
                “Recreation”,
                “Delivery”
        ],
        “onSale” : false,
        “name” : “Drovce Finnifield FR-7”,
        “price” : 87600,
        “weight” : “13 kilograms”,
        “additionalDetails” : {
                “material” : “polysterene”,
                “moreUses” : [
                        “Precision Agriculture”,
                        “Land Inspection”,
                        “Water Inspection”,
                        “Cinematography”
                ]
        }
}
  • Let us update values of all the documents of the utility field with the value from “Oil, gas, and mineral exploration” to “Natural Resource Exploration” using the positional $ (update) operator:
> db.drones.updateMany(
...    { utility : "Oil, gas, and mineral exploration" },
...    { $set: { "utility.$" : "Natural Resource Exploration" } }
... )
{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }



> db.drones.find().pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "utility" : [
                "Natural Resource Exploration",
                "Remote sensing",
                "Real estate and construction",
                "Recreation",
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "utility" : [
                "Natural Resource Exploration",
                "Remote sensing",
                "Real estate and construction",
                "Recreation",
                "Delivery"
        ],
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "utility" : [
                "Natural Resource Exploration",
                "Remote sensing",
                "Real estate and construction",
                "Recreation",
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "utility" : [
                "Natural Resource Exploration",
                "Remote sensing",
                "Real estate and construction",
                "Recreation",
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "utility" : [
                "Natural Resource Exploration",
                "Remote sensing",
                "Real estate and construction",
                "Recreation",
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        }
}

Updating Values in an Embedded Array

Let us look at an example updating the values of a nested array element using the positional $ (update) operator in MongoDB.

> db.drones.updateMany(
...    { price : { $gt : 80000 }, "additionalDetails.moreUses" : "Cinematography" },
...    { $set: { "additionalDetails.moreUses.$" : "Videography" } }
... )
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.drones.find().pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "utility" : [
                "Natural Resource Exploration",
                "Remote sensing",
                "Real estate and construction",
                "Recreation",
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "utility" : [
                "Natural Resource Exploration",
                "Remote sensing",
                "Real estate and construction",
                "Recreation",
                "Delivery"
        ],
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "utility" : [
                "Natural Resource Exploration",
                "Remote sensing",
                "Real estate and construction",
                "Recreation",
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "utility" : [
                "Natural Resource Exploration",
                "Remote sensing",
                "Real estate and construction",
                "Recreation",
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "utility" : [
                "Natural Resource Exploration",
                "Remote sensing",
                "Real estate and construction",
                "Recreation",
                "Delivery"
        ],
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        }
}

We have successfully updated the values of arrays using the $ (update) operator in MongoDB.

Read More: How to Connect MongoDB on Localhost 27017 in Windows – Starter’s Guide

Conclusion

In this article, we learned how to use the $update operator in MongoDB. Follow CodeForGeek for more such MongoDB and other tutorials.

Noteworthy References

MongoDB Docs

Aneesha S
Aneesha S
Articles: 171