I’ve got a document like this,
[
{
"id": 1,
"comments": [
{
"mail": "raj@gmail.com",
"Comment": "This product well worth the money"
}
{
"mail": "janu@gmail.com",
"Comment": "Excellent Product"
}
]
},
{
"id": 2,
"comments": [
{
"mail": "raj@gmail.com",
"Comment": "This product well worth the money"
}
{
"mail": "janu@gmail.com",
"Comment": "Excellent Product"
}
]
}
]
Now I need to change only the Comment with mail "raj@gmail.com" which is inside the comments array of the object having the id "1".
So the hierarchy looks like,
- Find Object with
id:1 - Find an object with
mail:raj@gmail.cominsidecommentsarray, - Update the
Commentof the particular Object
This is the query I tried to update but doesn’t work
modal.findOneAndUpdate(
{ id: 1, comments: { mail: "raj@gmail.com"} },
{
$set: {
"comments.$.Comment": "New Comment",
},
}
);
>Solution :
change comments: { mail: "raj@gmail.com"} to 'comments.mail': "raj@gmail.com"