I am having trouble with the "$in" operator in Mongoose. At a high level I have a User schema, and one of the fields is an array of a Card schema. Within the Card schema there is a 'score' field. I would like to update the 'score' field based on a list of Card ids. Here is what I am trying to use:
User.updateMany(
{
"_id": userId,
"cards._id": {
"$in": cardIds
}
},
{ $inc: {"cards.$.score": 1 }},
(err) => {console.log(err)}
)
When I run this code, only the first Card in the cardIds
array is updated instead of all of them. Any idea why this isn't working? Thanks.