如何使用Mongoose和ExpressJ删除MongoDB中的所有相关文档?

编程入门 行业动态 更新时间:2024-10-09 18:24:26

<a href=https://www.elefans.com/category/jswz/34/1771452.html style=如何使用Mongoose和ExpressJ删除MongoDB中的所有相关文档?"/>

如何使用Mongoose和ExpressJ删除MongoDB中的所有相关文档?

当用户删除博客帖子时,我试图删除对该博客帖子的所有评论和回复。我尝试了中间件,但它们非常混乱,似乎无法正常工作。有什么方法可以添加删除中间件,该中间件每次都被删除,无论删除博客的方法是什么。

这是我所有的模式-

博客帖子架构

var blogSchema = mongoose.Schema({
    name: { type: String, minlength: 3, maxlength: 256},
    body: { type: String},
    comments: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'comment'
    }],
    author: {
        type: mongoose.Schema.Types.ObjectId,
        refPath: 'user'
    }
}, {timestamps: true});

注释模式

var commentSchema = mongoose.Schema({
    body: { type: String, maxLength: 500 },
    replies: [{
        type: mongoose.Schema.Types.ObjectId,
        refPath: 'reply'
    }],
    author: {
        type: mongoose.Schema.Types.ObjectId,
        refPath: 'user'
    },
    target: {
        type: mongoose.Schema.Types.ObjectId,
        refPath: 'blog'
    }
}, {timestamps: true});

回复模式

var replySchema = mongoose.Schema({
    body: { type: String, maxLength: 500 },
    author: {
        type: mongoose.Schema.Types.ObjectId,
        refPath: 'user'
    },
    target: {
        type: mongoose.Schema.Types.ObjectId,
        refPath: 'comment'
    }
}, {timestamps: true});
回答如下:

您可以使用Ajax完成。 ==> Cannot delete article in Node/JavaScript app之类的其他内容可能对您有帮助。您可以根据需要更改codejs。

更多推荐

如何使用Mongoose和ExpressJ删除MongoDB中的所有相关文档?

本文发布于:2024-05-07 20:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1757183.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   文档   ExpressJ   Mongoose   MongoDB

发布评论

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

>www.elefans.com

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