I have a collection in mongodb with this doc:
{
"good_points":[
{
"title":"Good point 1",
"totalUsers":20
},
{
"title":"Good point 2",
"totalUsers":30
}
],
"bad_points":[
{
"title":"Bad point 1",
"totalUsers":20
},
{
"title":"Bad point 2",
"totalUsers":30
}
]
}
Now I have a use case where I would get an array of good points and bad points let’s say the array I recieved has these items:
const goodPoints = ["Good point 3", "Good point 1"];
const badPoints = ["Bad point 1", "Bad point 3"];
It can be any other array. What I have to do is to write a mongodb query that atomically does following -
If any good point in goodPoints already exists in the goodPoints array in collection then inc the totalUsers value of that item in goodPoints array by 1. Otherwise add a new item in the collection array with that good point.
But I am not able to do it after so many attempts.