Search code examples
node.jsreactjsmongodbmern

$pull data from a double nested schema


from the below schema ,

how do i delete out one specific answer comment ??


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)

I used the below code, to Add a comment in answers section and it worked fine...


const updatedQuestion = await Schemaa.updateOne(
            { 'answer._id' : answerId},              /// I passed that specific answer's _id   
            { $push: {                               ///  as answerId
           "answer.$.Comment": { 
             commentBody}
            } 
           })

regards.


Solution

  • caught the error, its working with this now...

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