Мой posts
Документ имеет следующую структуру:
{
"_id" : ObjectId("5d9df11b0e0a6e032bf3117f"),
"body" : "Sample content post.",
"date" : ISODate("2019-10-07T11:02:40.126Z"),
"comments" : [
{
"comment" : "comment on post",
"_id" : ObjectId("5d9df46e0e0a6e032bf31182"),
"replies" : [
{
"reply" : "reply to comment ",
"_id" : ObjectId("5d9bec26301798056bb07ab5")
},
]
},
],
}
Я хочу добавить новый reply
к конкретным post
и comments
этих данных запроса {data: req.body}
{
"data": {
"id_post": "5d9df11b0e0a6e032bf3117f",
"id_comment": "5d9df46e0e0a6e032bf31182",
"new_reply": "Another new reply to comment"
}
}
Я использую nodejs / express / mongoose, можете ли вы помочь, как мне добавить новый ответ.
router.post("/saveReply", function(req, res, next){
const query = Post.findById(req.body.id_post);
const updatePost = async () => {
try {
await Post.updateOne(
{
"_id": req.body.id_post,
"comments._id": req.body.id_comment
},
{
"$push": {
"comments.$.replies": {
"reply": req.body.reply,
}
}
},
);
}
catch (error) {
console.log('?', error);
}
};
updatePost().then(() => console.log('✅Post updated successfully!'));
});