如何在羽毛应用中的mongodb中同时使用相同的id删除两个不同集合中的文档

问题描述 投票:0回答:1

我正在尝试同时使用相同的ID删除两个不同集合中的文档。有可能吗?

用户集合:

{
    "_id" : ObjectId("5a310315f685dd5038fecaaa"),
    "userId" : 3,
    "accountId" : 1,
    "userType" : "DRIVER",
    "firstName" : "Karthi",
    "lastName" : "keyan",
    "email" : "[email protected]",
    "password" : "$2a$12$KFYc6riMnqTuzXhR0ssKZQmejAU4RF8FthAIQD4sgOUcALesp7DaJxK",
    "phone" : "xyz",
    "updatedAt" : ISODate("2017-12-13T10:38:13.492Z"),
    "createdAt" : ISODate("2017-12-13T10:38:13.492Z"),
    "__v" : 0
}

工人集合:

{
    "_id" : ObjectId("5a310315f685dd5038fecaab"),
    "workerId" : 1,
    "accountId" : 1,
    "name" : "Karthikeyan",
    "email" : "[email protected]",
    "mobile" : "xyz",
    "type" : "DRIVER",
    "joinDate" : ISODate("2017-12-13T10:38:13.070Z"),
    "assignedVehicleId" : "23423231",
    "licenseNumber" : "TN2506",
    "createdBy" : "1",
    "createdDate" : ISODate("2017-12-13T10:38:13.070Z"),
    "updatedBy" : "1",
    "updatedDate" : ISODate("2017-12-13T10:38:13.070Z"),
    "regularHours" : 3600,
    "regularRates" : 1500,
    "overtimeRates" : 400,
    "distanceRate" : 1000,
    "stopRate" : 50,
    "workerStatus" : "AVAILABLE",
    "userId" : 3,
    "__v" : 0
}

现在我想使用userId同时删除这两个文档。

javascript node.js mongodb mongodb-query feathersjs
1个回答
0
投票

数据库适配器允许使用remove调用null,并且查询将删除与该查询匹配的所有条目(请参阅the documentation)。在你的情况下:

app.service('users').remove(null, { query: { userId: 3 } });
app.service('workers').remove(null, { query: { userId: 3 } });

删除相关条目(例如,删除用户后删除该用户的所有工作人员)可以在after hook中完成:

app.service('users').hooks({
  after: {
    async remove(context) {
      const user = context.result;

      await context.app.service('workers').remove(null, {
        query: { userId: user.userId }
      });
    }
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.