I have this schema:
...
recommended:{
type:[{
movieId:String,
recommendedBy:[String]
}],
default:[],
},
name: {
type: String,
}
from the request I get movieId and id.
I want to add to recommended array a new object if there is no other item with the same movieId there already, and add theid to the recommendedBy nested array in both cases.
so far I have this:
const user = User.findByIdAndUpdate({_id:'123'},
{$AddToSet:{'recommended.$[movie].recommendedBy':id}},
{arrayFilters:[{'movie.movieId':movieId}]})
this will only set the recommendedBy of an already existing item with the same movieId,
but will not push a new object with the new movieId property.
(so it's basically $AddToSet for both arrays)
How could I achieve that? thanks!