I want to update an array document in a movie collection of mongoDB. However i can't pass the index i of the for loop as the property to update. e.g. it works when i update with "review.0.rating" but it doesn't if i update with review.i.rating. Any ideas??
for (var i=0; i< movie[0].review.length; i++){
//if the user made rating before, update his rating
if(movie[0].review[i].user === req.body.userID ){
await db.collection('movies').updateOne(
{_id:ObjectId(req.body.movieID)},
{$set: {review.i.rating: idx} }
);
}
}
Can you try this:
review[`${i}`].rating