I have to update an array inside a document on the basis of a string which i get from the req.value.
My collection
[
{
"_id": 1,
"key": [
"1-value-1",
"1-value-2"
]
},
{
"_id": 2,
"key": [
"2-value-1",
"2-value-2"
]
}
]
I have another string newString.
Let's assume two conditions:
- when
req.valueis in the array in that case replace the array value withnewString. - else
rew.valueis not present simple push thenewStringin the array.
Eg: I have a newString = 1-value-3 and req.value = 1-value-2 in that case replace array value 1-value-2 to 1-value-3.
Else if req.value isn't in array push newString value to array.
This is the collection on which i want to perform operations on.
Thanks for answering in advance.
update to 1-value-3 if 1-value-2 exists else push 1-value-3 if 1-value-4 doesn't existThat is unclear. Please edit the question to show exactly what the desired output shape should be.