最新版本的 mongoose 中删除级联?"/>
如何在没有 model.remove() 的最新版本的 mongoose 中删除级联?
无法删除级联,因为 Model.remove() 函数在猫鼬中不可用。 使用 findByIdAndDelete 不是调用中间件:
BootcampSchema.pre('remove',async function(next){ //before deleting the doc
console.log(`courses being removed from bootcamp ${this._id}`);
await this.model('Course').deleteMany({bootcamp:this._id});
next();
});
尝试了不同的删除方法都无法删除级联
回答如下:尝试使用
findOneAndRemove
:
BootcampSchema.findOneAndRemove({ _id: <id> });
BootcampSchema.pre('findOneAndRemove', ...);
更多推荐
如何在没有 model.remove() 的最新版本的 mongoose 中删除级联?
发布评论