【问题标题】:$pull data from a double nested schema$从双重嵌套模式中提取数据
【发布时间】: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


【解决方案1】:

发现错误,它现在正在处理这个......


await Questions.updateOne(
            { 'answer._id': answerId},{
                $pull:{
                    'answer.$.Comment' : { '_id' :commentId}
                }
            }
        )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-30
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多