簡體   English   中英

如何在羽毛應用程序的mongodb中同時使用相同的id刪除兩個不同集合中的文檔

[英]How to remove document in two different collection using same id at same time in mongodb on feathers application

我正在嘗試同時使用相同的ID刪除兩個不同集合中的文檔。 有沒有可能?

用戶集合:

{
    "_id" : ObjectId("5a310315f685dd5038fecaaa"),
    "userId" : 3,
    "accountId" : 1,
    "userType" : "DRIVER",
    "firstName" : "Karthi",
    "lastName" : "keyan",
    "email" : "karthikeyan.a1@gmail.com",
    "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" : "karthikeyan.a1@gmail.com",
    "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同時刪除這兩個文檔。

數據庫適配器允許調用帶有null remove和一個查詢,該查詢將刪除與該查詢匹配的所有條目(請參閱文檔 )。 在您的情況下:

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

刪除相關條目(例如,在刪除用戶后刪除該用戶的所有工作程序)可以在after 鈎子中完成

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

      await context.app.service('workers').remove(null, {
        query: { userId: user.userId }
      });
    }
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM