相关疑难解决方法(0)

在猫鼬中删除级联样式

有没有办法删除Mongoose中父级的所有子级,类似于使用MySQL的外键?

例如,在MySQL中,我将分配一个外键并将其设置为在删除时级联.因此,如果我要删除客户端,则也会删除所有应用程序和关联用户.

从顶层:

  1. 删除客户端
  2. 删除抽奖活动
  3. 删除提交

抽奖和提交都有一个client_id字段.提交的字段包含sweepstakes_id和client_id.

现在,我正在使用以下代码,我觉得必须有更好的方法.

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});

});
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb

39
推荐指数
2
解决办法
3万
查看次数

标签 统计

mongodb ×1

mongoose ×1