$[] All Positional Operator in MongoDB – Easy 2021 Guide

In this tutorial, I will talk about the $[] all positional operator for arrays in MongoDB.

The $[] all positional operator in MongoDB lets you update or modify all the elements inside the given array field while conducting an update operation.

In this guide, I will explain with different examples to use the $[] all positional operator in MongoDB so you can understand how to implement its usage in your wonderful application.

So, let us get started!

The $[] All Positional Operator Syntax in MongoDB

Let us take a quick look at the $[] positional operator syntax in MongoDB:

{ update operator: { "array.$[]" : value } }

This positional operator can be used with update operations like updateMany(), update(), and so on. Below is the syntax when used with an update operation:

db.collection.updateMany(
   { query conditions },
   { update operator: { "array.$[]" : value } }
)

Let us get started with the examples to learn how to use the $[] all positional operator in the Mongo shell.

Using the $[] All Positional Operator with a Non-Nested Array Field

Let us see how we can update arrays that are not nested or embedded inside documents in our collection.

  • Start up the Mongo local database server
  • Use the database you want to work with and navigate into it:
show dbs
use droneStore
  • Let us take a look at all the available documents in our collection:
> db.drones.find().pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "utility" : [
                "Combat",
                "Rescue",
                "Construction",
                "Cinematography",
                "Range-finding Photography",
                "Wildlife Monitoring"
        ],
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "utility" : [
                "Combat",
                "Rescue",
                "Construction",
                "Cinematography",
                "Range-finding Photography",
                "Wildlife Monitoring"
        ],
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "utility" : [
                "Combat",
                "Rescue",
                "Construction",
                "Cinematography",
                "Range-finding Photography",
                "Wildlife Monitoring"
        ],
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Land Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "utility" : [
                "Combat",
                "Rescue",
                "Construction",
                "Cinematography",
                "Range-finding Photography",
                "Wildlife Monitoring"
        ],
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "utility" : [
                "Combat",
                "Rescue",
                "Construction",
                "Cinematography",
                "Range-finding Photography",
                "Wildlife Monitoring"
        ],
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Videography"
                ]
        }
}
  • Now, we will use the $[] all positional operator to update all elements of our given array. I will use this operator with the $set operator:
> db.drones.updateMany(
...    { utility: "Wildlife Monitoring" },
...    { $set: { "utility.$[]": "Precision Agriculture" } },
...    { upsert: true }
... )

{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }
  • Let us check our documents:
> db.drones.find().pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "utility" : [
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture"
        ],
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "utility" : [
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture"
        ],
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "utility" : [
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture"
        ],
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Land Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "utility" : [
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture"
        ],
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "utility" : [
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture"
        ],
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Videography"
                ]
        }
}

Perfect all our elements in the given array field have been changed using the $[] all positional operator in MongoDB.

Using the $[] All Positional Operator with a Nested Array Field

Let us see how we can update arrays that are nested or embedded inside documents in our collection.

I will use the $set operator again and this time change the values of the moreUses field which is inside the additionalDetails field of all documents.

> db.drones.updateMany(
...    {  },
...    { $set: { "additionalDetails.moreUses.$[]": "Weather Forecast" } },
...    { multi: true }
... )
{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }
> db.drones.find().pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "utility" : [
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture"
        ],
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Weather Forecast",
                        "Weather Forecast",
                        "Weather Forecast"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "utility" : [
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture"
        ],
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Weather Forecast",
                        "Weather Forecast"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "utility" : [
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture"
        ],
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Weather Forecast",
                        "Weather Forecast"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "utility" : [
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture"
        ],
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Weather Forecast",
                        "Weather Forecast"
                ]
        }
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "utility" : [
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture",
                "Precision Agriculture"
        ],
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Weather Forecast",
                        "Weather Forecast"
                ]
        }
}

Perfect! We have successfully selected and used the all positional operator to update all elements of the given array field.

Read More: $pullAll Operator in MongoDB: Easy Guide

Conclusion

In this article, we learned how to use the $[] all positional operator in MongoDB. To learn further, follow CodeForGeek.com!

Noteworthy References

https://docs.mongodb.com/manual/reference/operator/update/positional-all/

Aneesha S
Aneesha S
Articles: 171