How can I insert an value in "total" array where pid = "123"
{
"_id" : ObjectId("625bc1983016ed5208bbdf90"),
"NAME" : "XYZ",
"data" : [
{
"pid" : "123",
"total" : [ ]
},
{
"pid" : "456",
"total" : [ ]
}
]
}
CodePudding user response:
The right way is:
db.collection.update({
data: {
$elemMatch: {
pid: "123"
}
}
},
{
$set: {
"data.$.buyers": "some name"
}
})
$elemMatch allows you to match more than one component within the same array element and then you can use $set
to update the specific element.
You can run it here to check the same query https://mongoplayground.net/p/lveZw-8wkZ0
You can also have a look at documentation of $elemMatch
in this link.
Let me know if you face any issue.