I have the following model:
user :{
overall_stats: {
elo: {
type: Array,
default: {
date: new Date(),
ranking: 1000,
}
},
nrGames: {
...
},
nrWins: {
...
},
winRate: {
...
},
},
.
.
.
}
Now I want to push a new entry: { ranking: 1020, date: 2020-02-26T18:39:22.933Z }to the elo array. I tried different versions below but the elo property is completely gone after executing the function. nrGames and the others are updated correctly.
async function updateUserStatsAfterGameInDB(userId, newElo, numberOfGames, numberOfWins, winRate) {
console.log(newElo);
return await User.findOneAndUpdate(
{
_id: userId // search query
},
{
overall_stats : {
$push: {elo : newElo},
nrGames : numberOfGames,
nrWins : numberOfWins,
winRate : winRate
}
},
{
new: true, // return updated doc
runValidators: true, // validate before update
useFindAndModify: false
}
)
}
Where is the error?