0
嘿我有一個問題,從我的數據庫中刪除嵌套數組,我想使用findOneAndUpdate
和$pull
。我的意思是從reply
陣列中刪除項目。我嘗試通過_id
找到評論項目並刪除此回覆項目,但這不起作用。請看下面我的代碼。
我的架構
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
})