Updating a nested Array in using UpdateOne()
As there's two nested arrays in your document, you can't set the field with classic positional operator '$'.
Instead, you should use the arrayFilters option like this:
db.collection.update({
"_id": ObjectId("6032a5ad80443334a35f2232")
},
{
$set: {
"List.$[list].customData.$[customData].systemUpdate_DT": "updatedDTTM"
}
},
{
"multi": false,
"upsert": false,
arrayFilters: [
{
"list._id": {
"$eq": ObjectId("6032a5af80443334a35f2234")
}
},
{
"customData._id": {
"$eq": ObjectId("6032a5bc80443334a35f223c")
}
}
]
})
try it online: mongoplayground.net/p/fb_86rNUKvt
How can I update a multi level nested array in MongoDB?
Since Mongo 3.6, you can update multi-nested arrays by combining the following operators:
$set
(to update a specific field)$[]
(to match any item in an array)$[<identifier>]
(to match specific items in an array)
Example
Here's how you can update a specific proyectos
document that has a reuniones
array that has a participantes
array that has an object with the field nomina
equal to 2
:
// update a specific proyectos document
// that has a field "reuniones" which is an array
// in which each item is an object with a field "participantes" that is an array
// in which each item is an object that has a field "nomina" equal to 2
db.proyectos.update({
_id: ObjectId("5bfa09f0a0441f38d45dcc9c"),
}, {
$set: {
"reuniones.$[].participantes.$[j].firma": <your update>
},
}, {
arrayFilters: [
{
"j.nomina": 2
}
]
})
If you wanted to limit your query to a specific reunion
, you can do:
db.proyectos.update({
_id: ObjectId("5bfa09f0a0441f38d45dcc9c"),
}, {
$set: {
"reuniones.$[i].participantes.$[j].firma": <your update>
},
}, {
arrayFilters: [
{
"i._id": ObjectId("5bfa09f0a0441f38d45dcc99")
},
{
"j.nomina": 2
}
]
})
To update all proyectos
satisfying the above condition, just omit the _id
query:
// update all proyectos
// that have a field "reuniones" which is an array
// in which each item is an object with a field "participantes" that is an array
// in which each item is an object that has a field "nomina" equal to 2
db.proyectos.update({}, {
$set: {
"reuniones.$[].participantes.$[j].firma": <your update>
},
}, {
arrayFilters: [
{
"j.nomina": 2
}
]
})
MongoDB - How to find and update elements in a nested array
Think that MongoDB Update with Aggregation Pipeline fulfills your scenario.
$set
- Setdata.category
value.1.1.
$map
- Iterate each element indata.caetgory
and return an array.1.1.1.
$mergeObjects
- Merge current document with the document withsubcategory
field from 1.1.1.1.1.1.1.1
$map
- Iterate each value fromsubcategory
array. With$cond
to replace the wordEDUCATION
withSPORTS
if fulfill, else use existing value ($$this
).
db.employees.updateMany({
"data.category.subcategory": "EDUCATION"
},
[
{
"$set": {
"data.category": {
$map: {
input: "$data.category",
in: {
$mergeObjects: [
"$$this",
{
subcategory: {
$map: {
input: "$$this.subcategory",
in: {
$cond: {
if: {
$eq: [
"$$this",
"EDUCATION"
]
},
then: "SPORTS",
else: "$$this"
}
}
}
}
}
]
}
}
}
}
}
]
Sample Mongo Playground
How to update a value in a array of nested object in Mongoose(Momgodb)?
You can update a value in an array of nested objects in Mongoose
`db.users.update({"pm.checkList.ch_id" : "621eff4e0ed5c751adaa42fb"},{ $set:{
"pm.checkList.$.val" : "nop",
"pm.checkList.$.remarks" : "bed",
}})`
Update value of key in Object in nested array of objects in MongoDB
Based on this great answer by @R2D2, you can do:
db.collection.update({
"array1.array2._id": ObjectId("627a6fab60dc3c523b396af1")
},
{
$set: {
"array1.$[].array2.$[y].name": "John"
}
},
{
arrayFilters: [
{
"y._id": ObjectId("627a6fab60dc3c523b396af1")
}
]
})
As you can see on this playground example
Related Topics
How to Sort an Array of Objects by Multiple Fields
Client on Node.Js: Uncaught Referenceerror: Require Is Not Defined
Format Number to Always Show 2 Decimal Places
Is JavaScript Guaranteed to Be Single-Threaded
How to Get the Current Date in JavaScript
Calculate Age Given the Birth Date in the Format Yyyymmdd
JavaScript: Client-Side Vs. Server-Side Validation
How to Distinguish Between Left and Right Mouse Click With Jquery
Programmatically Navigate Using React-Router
Why Can't I Directly Modify a Component'S State, Really
Innertext' Works in Ie, But Not in Firefox
Escape String For Use in JavaScript Regex
Remove Empty Elements from an Array in JavaScript
Should All Jquery Events Be Bound to $(Document)