MongoDB 3.6 allow to perform complex array manipulations against all matching elements in any array – no matter how deeply nested – Update Nested Arrays with $[identifier]
Consider the document below on survey collection:
{
"_id": "5a7d86d8fac139e71b0b9f5b",
"results": [
{
"items": [
{
"comments": [
{
"id" : "123456",
"email": "[email protected]",
"content": "comment 1",
"createdAt": "2018-05-03"
"replies": []
}
]
}
]
}
]
}
I'm trying to update the comment email, content and createdAt fields and without touching to the id and replies fields.
I'm using $set and the new $[<identifier>]
I tried the command below based on The update command :
db.runCommand({
update: "survey",
updates: [
{
q: {},
u: {
$set: {
"results.$[].items.$[].comments.$[comment]": {
"email": "[email protected]",
"content": "comment 2",
"createdAt": "2018-05-04"
}
}
},
arrayFilters: [
{
"comment.id": {
$eq: "123456"
}
}
]
}
]
})
The command works but it removes the id and replies fields.
Any help ? Thanks.