mongodb Mongoose 中的级联样式删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14348516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cascade style delete in Mongoose
提问by Nick Parsons
Is there a way to delete all children of an parent in Mongoose, similar to using MySQLs foreign keys?
有没有办法删除 Mongoose 中父级的所有子级,类似于使用 MySQL 的外键?
For example, in MySQL I'd assign a foreign key and set it to cascade on delete. Thus, if I were to delete a client, all applications and associated users would be removed as well.
例如,在 MySQL 中,我会分配一个外键并将其设置为在删除时级联。因此,如果我要删除一个客户端,所有应用程序和关联的用户也将被删除。
From a top level:
从顶层:
- Delete Client
- Delete Sweepstakes
- Delete Submissions
- 删除客户端
- 删除抽奖活动
- 删除提交
Sweepstakes and submissions both have a field for client_id. Submissions has a field for both sweepstakes_id, and client_id.
抽奖和提交都有一个用于 client_id 的字段。提交有一个用于sweettakes_id 和client_id 的字段。
Right now, I'm using the following code and I feel that there has to be a better way.
现在,我正在使用以下代码,我觉得必须有更好的方法。
Client.findById(req.params.client_id, function(err, client) {
if (err)
return next(new restify.InternalError(err));
else if (!client)
return next(new restify.ResourceNotFoundError('The resource you requested could not be found.'));
// find and remove all associated sweepstakes
Sweepstakes.find({client_id: client._id}).remove();
// find and remove all submissions
Submission.find({client_id: client._id}).remove();
client.remove();
res.send({id: req.params.client_id});
});
回答by JohnnyHK
This is one of the primary use cases of Mongoose's 'remove'
middleware.
这是 Mongoose'remove'
中间件的主要用例之一。
clientSchema.pre('remove', function(next) {
// 'this' is the client being removed. Provide callbacks here if you want
// to be notified of the calls' result.
Sweepstakes.remove({client_id: this._id}).exec();
Submission.remove({client_id: this._id}).exec();
next();
});
This way, when you call client.remove()
this middleware is automatically invoked to clean up dependencies.
这样,当你调用client.remove()
这个中间件时会自动调用清理依赖。
回答by Talha Awan
In case your references are stored other way around, say, client
has an array of submission_ids
, then in a similar way as accepted answer you can define the following on submissionSchema
:
如果您的引用以其他方式存储,例如,client
有一个数组submission_ids
,那么以与接受的答案类似的方式,您可以在 上定义以下内容submissionSchema
:
submissionSchema.pre('remove', function(next) {
Client.update(
{ submission_ids : this._id},
{ $pull: { submission_ids: this._id } },
{ multi: true }) //if reference exists in multiple documents
.exec();
next();
});
which will remove the submission'sid from the clients'reference arrays on submission.remove()
.
这将从 上的客户端引用数组中删除提交的id 。submission.remove()
回答by Sam Bellerose
Here's an other way I found
这是我发现的另一种方式
submissionSchema.pre('remove', function(next) {
this.model('Client').remove({ submission_ids: this._id }, next);
next();
});