'Update MongoDB field using value of another object array field from current document
I have this data:
{
"_id" : ObjectId("5fe9b45d3613f35f9c7fe013"),
"capitainSide" : "",
"soldierSide" : "",
"status" : "creation",
"type" : "invitations",
"invitedUsers" : [ObjectId("5fceb82c2917434bb8ef4966")],
"members" : [
{
"_id" : ObjectId("5fb71aebc0f0ea402cd8e007"),
"role" : "capitain",
"side" : "black",
}
]
}
and I want to get this result:
{
"_id" : ObjectId("5fe9b45d3613f35f9c7fe013"),
**"capitainSide" : "black"**
**"soldierSide" : "white"**
"status" : "creation",
"type" : "invitations",
"invitedUsers" : [ObjectId("5fceb82c2917434bb8ef4966")],
"members" : [
{
"_id" : ObjectId("5fb71aebc0f0ea402cd8e007"),
"role" : "capitain",
"side" : "black",
},
{
"_id" : ObjectId("5fb71aebc0f0ea402cd8e008"),
"role" : "soldier",
"side" : "white",
}
]
}
Actually, I want to update 'capitainSide' field with the value of members array where the role = "capitainSide" in the value of 'side' field, and the same to 'soldierSide' field
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
