【发布时间】:2022-01-30 12:31:30
【问题描述】:
从下面的架构,
如何删除一个特定的回答评论??
const Schemaa = mongoose.Schema({
questionBody: String,
Comment:[{
commentBody: String,
}],
answer: [{
answerBody: String,
Comment:[{
commentBody:String, /// i want to delete one specific comment having a unique
}] /// 'Object _id.
}]
})
export default mongoose.model("Questions", Schemaa)
我使用下面的代码,在答案部分添加评论,效果很好......
const updatedQuestion = await Schemaa.updateOne(
{ 'answer._id' : answerId}, /// I passed that specific answer's _id
{ $push: { /// as answerId
"answer.$.Comment": {
commentBody}
}
})
问候。
【问题讨论】:
-
你知道你可以将条件传递给$pull
-
是的,我也使用它...删除特定的
answer,但由于删除answer comment是双重嵌套的,我仍在努力解决这个问题:( -
尝试使用与 $push 相同的字段名称进行 $pull
-
是的……它现在可以工作了,非常感谢……(实际上我在 index.js 中使用了
/comment/deleteacomment/:id,而我在控制器部分写错了……得到 404 )
标签: node.js reactjs mongodb mern