2016-09-16 58 views
1

我想禁用在某些文檔模型中刪除文檔。 內部文檔前鉤我可以做財產以後這樣的:mongoose模型禁用刪除

someSchema.pre('remove', function (next) { 
    let err = new Error('Delete docs is not allowed!'); 
    next(err); 
}); 

但除去仍然可以通過someModel.remove() 執行我怎樣才能避免這種情況?

回答

0

的回答很簡單:只需重寫刪除這樣的樂趣:

let model = mongoose.model('doc', someDocSchema); 
model.remove = function() { 
    throw new Error('Delete docs is not allowed!'); 
}; 
+1

另外還有'findOneAndRemove','findByIdAndRemove'和'Model.find()刪除()' – robertklep