如何正确删除MongoDB中的孤立引用?(How to properly delete an orphaned reference in MongoDB?)

编程入门 行业动态 更新时间:2024-10-27 15:21:51
如何正确删除MongoDB中的孤立引用?(How to properly delete an orphaned reference in MongoDB?)

所以,我正在Node中构建一个类似博客的小项目,我遇到了孤立数据库引用的问题。 我在两个相互引用的文件中有两个模型。

这是我的模特:

// ./models/user Var UserSchema = mongoose.Schema({ name: String, posts: [{type: mongoose.SchemaTypes.ObjectId, ref:'Post'}] }); // ./models/post var PostSchema = mongoose.Schema({ title:String, post_body: String, posted_by: mongoose.SchemaTypes.ObjectId });

我的问题是当你删除say,post,你将如何删除用户的帖子数组中的引用? 我的想法是我可以创建一个在删除路由之前运行的中间件,并在我实际删除帖子之前删除User的post数组中的引用。 这会被认为是最好的方式吗? 我在Stack上发现了一个帖子,它在模式中使用了'pre'函数,如下所示:

// ./models/post PostSchema.pre('remove', function(next){ this.model('User').remove({posts: this._id}, next); });

这是实际的堆栈帖子: 在MongoDB中删除时自动删除引用对象 。 我不能得到这项工作。 但是,我确实实现了一个自定义中间件来删除引用,但感觉它可能不是最佳实践。 任何提示/建议将不胜感激。 谢谢!

So, I am building a small blog-like project in Node, and I am running into an issue with orphaned database references. I have two models in separate files that reference each other.

Here are my models:

// ./models/user Var UserSchema = mongoose.Schema({ name: String, posts: [{type: mongoose.SchemaTypes.ObjectId, ref:'Post'}] }); // ./models/post var PostSchema = mongoose.Schema({ title:String, post_body: String, posted_by: mongoose.SchemaTypes.ObjectId });

My question is when you delete say, a Post, how would you delete the reference in the User's post array? My thinking was I could create a middleware to run before the delete route and delete the reference in the User's post array before I actually delete the post. Would that be considered the best way to go about it? I found a post on Stack that uses a 'pre' function in the schema like this:

// ./models/post PostSchema.pre('remove', function(next){ this.model('User').remove({posts: this._id}, next); });

Here is the actual stack post: Automatically remove referencing objects on deletion in MongoDB . I could not get this work though. I did ,however, implement a custom middleware to delete the references, but feel it might not be best practice. Any tips/advice would be greatly appreciated. Thanks!

最满意答案

你不想在这里使用.remove() ,但你需要使用$pull取代.update() :

PostSchema.pre('update',function(next) { this.model('User').update( { }, { "$pull": { "posts": this._id } }, { "multi": true }, next ); })

这是从数组中删除内容的正确操作。 “multi”确保为所有引用它的User对象删除“post”,认为它可能实际上只是一个文档。

.remove()方法用于“删除”整个文档。 .update()方法对文档进行“更改”。

You don't want .remove() here but you want .update() with $pull instead:

PostSchema.pre('update',function(next) { this.model('User').update( { }, { "$pull": { "posts": this._id } }, { "multi": true }, next ); })

That's the correct operation to remove something from an array. The "multi" makes sure that the "post" would be removed for all User objects that reference it, thought it probably really is only one document anyway.

The .remove() method is for "removing" whole documents. The .update() method makes "changes" to documents.

更多推荐

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

发布评论

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

>www.elefans.com

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