简体   繁体   English

在猫鼬中使用“deleteMany”发布中间件

[英]Using "deleteMany" post middleware in mongoose

In my Nodejs and Express app, I have a mongoose User schema, a Post schema and a Comment schema as follows:在我的 Nodejs 和 Express 应用程序中,我有一个 mongoose User 架构、一个 Post 架构和一个 Comment 架构,如下所示:

const UserSchema = new Schema({
    username: {
        type: String,
        required: true,
        unique: true
    },
    password: String,
    posts : [
        {
            type : mongoose.Schema.Types.ObjectId,
            ref  : 'Post' 
        }
    ]
});


const PostSchema = new Schema({
    author : {
        type : mongoose.Schema.Types.ObjectId,
        ref  : 'User' 
    },
    createdAt: { type: Date, default: Date.now },
    text: String,
    comments : [
        {
            type : mongoose.Schema.Types.ObjectId,
            ref  : 'Comment' 
        }
    ],
});

const CommentSchema = new Schema({
    author : {
        type : mongoose.Schema.Types.ObjectId,
        ref  : 'User' 
    },
    createdAt: { type: Date, default: Date.now },
    text: String
});

I have coded the general CRUD operations for my User.我已经为我的用户编写了一般的 CRUD 操作。 When deleting my user, I can easily delete all posts associated with that user using deleteMany :删除我的用户时,我可以使用deleteMany轻松删除与该用户关联的所有帖子:

Post.deleteMany ({ _id: {$in : user.posts}});

To delete all the comments for all the deleted posts, I can probably loop through posts and delete all the comments, but I looked at mongoose documentation here and it seems that deleteMany function triggers the deleteMany middleware .要删除所有已删除帖子的所有评论,我可能会遍历帖子并删除所有评论,但我查看了mongoose文档here ,似乎deleteMany函数触发了deleteMany 中间件 So In my Post schema, I went ahead and added the following after defining schema and before exporting the model.因此,在我的 Post 架构中,我继续在定义架构之后和导出模型之前添加了以下内容。

PostSchema.post('deleteMany', async (doc) => {
    if (doc) {
        await Comment.deleteMany({
            _id: {
                $in: doc.comments
            }
        })
    }
})

When deleting user, this middleware is triggered, but the comments don't get deleted.删除用户时,会触发此中间件,但不会删除评论。 I got the value of doc using console.log(doc) and I don't think it includes what I need for what I intend to do.我使用console.log(doc)获得了doc的价值,但我认为它不包括我打算做的事情所需的内容。 Can someone tell me how to use the deleteMany middleware properly or if this is not the correct path, what is the most efficient way for me to delete all the associated comments when I delete the user and their posts?有人可以告诉我如何正确使用deleteMany中间件,或者如果这不是正确的路径,当我删除用户及其帖子时,删除所有相关评论的最有效方法是什么?

deleteMany will not give you access to the affected document because it's a query middleware rather than a document middleware (seehttps://mongoosejs.com/docs/middleware.html#types-of-middleware ). deleteMany不会让您访问受影响的文档,因为它是查询中间件而不是文档中间件(请参阅https://mongoosejs.com/docs/middleware.html#types-of-middleware )。 Instead it returns the "Receipt-like" object where it tells it successfully deleted n objects and such.相反,它返回“Receipt-like”对象,它告诉它成功删除了 n 个对象等。

In order for your hook to work as expected, you'll need to use something other than deleteMany , such as getting all of the documents (or their IDs), and loop through each one, using deleteOne .为了让您的钩子按预期工作,您需要使用deleteMany以外的其他deleteMany ,例如获取所有文档(或它们的 ID),并使用deleteOne遍历每个deleteOne

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM