I am making a voting system in which an object will have an array called scores, every time it is qualified a record will be entered to this array as long as the same id does not exist.
Sample Doc :
{
name:"jose luis",
scores:[]
}
The user push :
{id:1,stars:5}
Updated Doc :
{
name:"jose luis",
scores:[{id:1,stars:5}]
}
The user push 2nd time :
{id:1,stars:4}
Updated Doc :
{
name:"jose luis",
scores:[{id:1,stars:4}]
} //this should update the value of the stars under the array element with the existing id.
I have tried this :
Politico.update(
{ name: "jose luis" },
{
$setOnInsert: { "$scores.id": body.req.id},
"$addToSet": {
scores: {
"id": body.req.id,
"starts": body.req.stars
}
}
},
{ upsert: true }
I have tried this but it doesn't work, how can I fix it? Thanks a lot.