最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何在没有 model.remove() 的最新版本的 mongoose 中删除级联?

网站源码admin32浏览0评论

如何在没有 model.remove() 的最新版本的 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', ...);
发布评论

评论列表(0)

  1. 暂无评论