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

编程入门 行业动态 更新时间:2024-10-06 08:33:53

如何在没有 model.remove() 的<a href=https://www.elefans.com/category/jswz/34/1770937.html style=最新版本的 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 中删除级联?

本文发布于:2024-05-30 14:29:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770595.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最新版本   级联   如何在   remove   model

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!