I have a document which holds an array property having object elements.
E.G.
{
"_id": "61c01098bc29520008efc00f",
"name": "codedump",
"employments": [
{
"type": "PERMANENT",
"salary": 1000000000
},
{
"type": "TEMP",
"salary": 1000000000
},
{
"type": "PART_TIME",
"salary": 1000000000
},
]
}
And while updating the employment salary using type if the type is already in employments then i need to update the salary only. Else I need to push the new item to the array. I'm trying to find a way to make this work in a single query.
- If the type is already present in array(for update):
/// Update data:
{
"type": "PERMANENT",
"salary": 10
}
// For this case I want the example document to become:
{
"_id": "61c01098bc29520008efc00f",
"name": "codedump",
"employments": [
{
"type": "PERMANENT",
"salary": 10 // From 1000000000 to 10
},
{
"type": "TEMP",
"salary": 1000000000
},
{
"type": "PART_TIME",
"salary": 1000000000
},
]
}
- If the type is not present:
/// Update data:
{
"type": "INVESTMENT",
"salary": 10000
}
// For this case I want the example document to become:
{
"_id": "61c01098bc29520008efc00f",
"name": "codedump",
"employments": [
{
"type": "PERMANENT",
"salary": 1000000000
},
{
"type": "TEMP",
"salary": 1000000000
},
{
"type": "PART_TIME",
"salary": 1000000000
},
{
"type": "INVESTMENT",
"salary": 10000
}
]
}