For the document matching the criteria _id equal to 100, the following operation update the value second element (array index of 1) in the tags field and the rating field in the first element (array index of 0) of the ratings array.
db.products.update(
{ _id: 100 },
{ $set:
{
"tags.1": "rain gear",
"ratings.0.rating": 2
}
}
)
The above is a quote from the mongodb $set documentation, how would I go about adding a variable for the index so instead of "tags.1" it becomes "tags."+index
For example, if index is equal to 7 it would return the equivalent of "tags.7".
This is my code:
Users.findOneAndUpdate({'username': post.username}, {
$set: {
"notifications.0": {
'id': post._id, 'message': post.message, 'amount': amount
}
}
}, function () {})
It works perfectly but only because I hardcoded the 0 as the index. I want to replace that hardcoded 0 with an index variable.
"notifications.0"with"notifications."+index?