2017-03-06 57 views
0

我有架構貓鼬

var postSchema = new Schema({ 
    comments: [{ 
     nickName: {type: String, required: true}, 
     comment: {type: String, required: true} 
    }] 
}); 

然後我想拉的意見

Post.findOneAndUpdate({'_id': req.body._id}, {$pull: {'comments': {'_id': req.body._idComment} }}, 
    function(err, data) { 
    if (err) { 
     res.send({status:400}); 
    } else { 
     // how get $pull object if I don't know index of element of array 
     // data returns all objects 
    } 
}); 

回答

0

沒有直接的方法,這樣做一個評論。但是它沒有什麼解決方法。 更新數據(我的意思是刪除它),如果它成功,那麼你知道你的輸入刪除元素是你的答案。

Post.update({'_id': req.body._id}, 
    {$pull: {'comments': {'_id': req.body._idComment} }},function(err, data) { 
     .... 
     .... 
}); 

您也可以運行find查詢來檢查,如果你的數據被刪除也將與您確認刪除的數據。

否則findOneAndUpdate使用{new:True}選項返回更新的文檔,您可以將輸入與返回的文檔進行比較以獲取刪除的數據。