My data structure looks like this:
{
"_id" : .....,
"topicId" : topicId1,
"category": .....,
"other things"....,
"posts": [
{postId:id1,
username:.....,
postBody: 'some text here'
},
{postId:id2,
username:.....,
postBody: ' some other text here'
}
]
}
My goal is to find a document where topicId equal to some Id, after that in the document find in posts an object where its postId equal to some Id, and finally update postBody in that object.
At the end it should look like this:
{
"_id" : .....,
"topicId" : topicId1,
"category": .....,
"other things"....,
"posts": [
{postId:id1,
username:.....,
postBody: 'some text here'
},
{postId:id2,
username:.....,
postBody: 'HERE IS UPDATED TEXT'
}
]
}
This is how i'm deleting a post:
db.collection('topics').findOneAndUpdate({ 'topicId': topicId }, { $pull: { 'posts': { 'postId': postId } } })
But i have no idea how to update it based on my data structure. I'd appreciate any help.