2017-12-03 3 views
0

Hey, ich habe ein Problem mit verschachtelten Array aus meiner Datenbank zu entfernen, möchte ich findOneAndUpdate und $pull verwenden. Mein Punkt ist, Artikel aus reply Array zu entfernen. Ich versuche, Kommentare von _id finden und entfernen Sie diese Antwort, aber das funktioniert nicht. Bitte schau auf meinen Code unten.

mein Schema

var productSchema = new Schema({ 

description: [{ 
    type: String, 
    require: true, 
}], 
comments: [{ 
    body: { 
     type: String, 
     trim: true, 
    }, 
    author: { 
     type: String, 
    }, 
    date: { 
     type: Date, 
    }, 
    reply: [{ 
     body: { 
      type: String, 
      trim: true, 
     }, 
     author: { 
      type: String, 
     }, date: { 
      type: Date, 
     } 
    }] 
    }] 
}); 

api

router.put('/dashboard/admin/komentarz/odpowiedz/usun/', function(req, res) { 
var currentCourse = req.body._id; // main item id 
var deleteReply = req.body.reply; // reply item id 
var commentId = req.body.commentId // comments item id 
Product.findOneAndUpdate({ _id: currentCourse }, { $pull: { reply: req.body.reply }}, function(err, product){ 
    //some code 
}) 

Antwort

1

ref Nehmen von Mongoose, pull from subdocument

Product.findOneAndUpdate({ 
    _id: currentCourse, 
    // make sure sub document have a unique field let take _id 
    "comments._id" : 'commentId' 
}, 
{ 
    $pull: { 
     "comments.$.reply": { 
      _id:'replyId' 
     } 
    } 
}, 
{ 
    upsert:false, 
    new:true 
}, function(err, product){ 
    //some code 
}) 
Verwandte Themen