How to Update Array Value Of Array Field Using MongoDB

➤ Update Index Array Value Using MongoDB.

The "$" symbol in MongoDB is used as a placeholder for a value in a document. It can be used in various MongoDB operations such as update, find, or aggregate, to represent a field value in a document. For example, in an update operation, you can use "$" to reference the current value of a field, allowing you to make modifications to it. It's worth noting that the "$" symbol is a reserved character in MongoDB and should not be used in field names, as it has a special meaning.

⇒ What Is Say's below Example Here :

● Suppose we have one database that name is `UsersDb`. UsersDb have a collection that is `Users`. User Collection has 10 documents and documents has key and values like.

db.users.insertMany([{
  "_id":ObjectId("63da4eddc6e9bca274fb3ad7"),
  "parents":[
  	ObjectId("63da4ef89823cfbace84397b"),
  	ObjectId("63da4f00adcb44087a86c19a"),
  	ObjectId("63da4f055906af460f1e7789"),
  	ObjectId("63da4f09886df825a65bce10")
  ]
  },{
  "_id":ObjectId("63da4f2e4ad4cd487c24c0d7"),
  "parents":[
  	ObjectId("63da4f33ca8ff8bf8d9944d2"),
  	ObjectId("63da4f391614457b86039cfd"),
  	ObjectId("63da4f3fb4db5047c375e738"),
  	ObjectId("63da4f09886df825a65bce10")
  ]
  },{
  "_id":ObjectId("63da504f312ad0a0011dc4bd"),
  "parents":[
  	ObjectId("63da50562e4544a05523d5a8"),
  	ObjectId("63da505b32ea6dbcb0385d7f"),
  	ObjectId("63da5062b45b6a1eb7918bbe"),
  	ObjectId("63da4f09886df825a65bce10")
  ]
  }])

● We want to update all array value that have value ObjectId("63da4f09886df825a65bce10")

● Update to ObjectId("63da51004d747b3b312db320").

➤ Example Of The Operation :


db.us.updateMany(
{
 "parents":ObjectId("63da4f09886df825a65bce10")
},
{
 "$set":{
  "parents.$":ObjectId("63da51004d747b3b312db320")
}
});  

⇒ Final Output Like Is :

db.users.insertMany([{
"_id":ObjectId("63da4eddc6e9bca274fb3ad7"),
"parents":[
   ObjectId("63da4ef89823cfbace84397b"),
   ObjectId("63da4f00adcb44087a86c19a"),
   ObjectId("63da4f055906af460f1e7789"),
   ObjectId("63da51004d747b3b312db320")
  ],
"partnership":[
 { "_id":ObjectId("63da5477208f71646346118f")},
 {"_id":ObjectId("63da5464fb6d8b2448575d24")}
]
  },{
  "_id":ObjectId("63da4f2e4ad4cd487c24c0d7"),
  "parents":[
  	ObjectId("63da4f33ca8ff8bf8d9944d2"),
  	ObjectId("63da4f391614457b86039cfd"),
  	ObjectId("63da4f3fb4db5047c375e738"),
  	ObjectId("63da51004d747b3b312db320")
  ],
   "partnership":[
  	{
    	"_id":ObjectId("63da5477208f71646346118f")
    },
    {
    	"_id":ObjectId("63da54715aa0c88d9b674d1d")
    }
  ]
  },{
  "_id":ObjectId("63da504f312ad0a0011dc4bd"),
  "parents":[
  	ObjectId("63da50562e4544a05523d5a8"),
  	ObjectId("63da505b32ea6dbcb0385d7f"),
  	ObjectId("63da5062b45b6a1eb7918bbe"),
  	ObjectId("63da51004d747b3b312db320")
  ],
   "partnership":[
  	{
    	"_id":ObjectId("63da5477208f71646346118f")
    },
    {
    	"_id":ObjectId("63da547c39ac2bbdfdca12b4")
    }
  ]
  }])

➤ Update Object Array Value Using MongoDB.



db.settlement.updateMany(
{
 "partnership._id":ObjectId("63da5477208f71646346118f")
},
{
 "$set":{
  "partnership.$._id":ObjectId("63da5666f4f040427f777a78")
  }
});
   
    

Comments