I have this document structure
{
"_id": <OBJECT_ID>,
"orders": [
{
"userId": 1,
"handleName": "John Doe",
"orderAmount": 3,
"others": [
{
"userId": 1,
"handleName": "John Doe"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 2,
"handleName": "Maria Gigante",
"orderAmount": 2,
"others": [
{
"userId": 1,
"handleName": "John Doe"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 1,
"handleName": "John Doe",
"orderAmount": 4,
"others": [
{
"userId": 1,
"handleName": "John Doe"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 3,
"handleName": "John Doe",
"orderAmount": 4,
"others": [
{
"userId": 1,
"handleName": "John Doe"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
}
]
}
I would like to update all the objects that has the userId
of 1
and handleName
of John Doe
.
Note that objects can have the same handle names but different user id. That is why I need to match it with the userId
first.
For example I want to change the handle name to Donald Stark
I would like to have this result:
{
"_id": <OBJECT_ID>,
"orders": [
{
"userId": 1,
"handleName": "Donald Stark",
"orderAmount": 3,
"others": [
{
"userId": 1,
"handleName": "Donald Stark"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 2,
"handleName": "Maria Gigante",
"orderAmount": 2,
"others": [
{
"userId": 1,
"handleName": "Donald Stark"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 1,
"handleName": "Donald Stark",
"orderAmount": 4,
"others": [
{
"userId": 1,
"handleName": "Donald Stark"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 3,
"handleName": "John Doe",
"orderAmount": 4,
"others": [
{
"userId": 1,
"handleName": "Donald Stark"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
}
]
}
How is this achieved?
Update: I forgot that I also have another inner array that I also want to be updated.