2017-12-14 6 views
0

同じIDを使用して2つの異なるコレクション内のドキュメントを同時に削除しようとしています。何か可能性はありますか?feathersアプリケーションのmongodbで同時に同じidを使用して2つの異なるコレクションのドキュメントを削除する方法

ユーザーコレクション:

{ 
    "_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 
} 

今私がユーザIDを使用して、同時にこれら二つの文書を削除したいです。

答えて

0

removenullと呼び、そのクエリに一致するすべてのエントリを削除するクエリ(the documentationを参照)をデータベースアダプタで呼び出すことができます。あなたのケースでは:

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

     await context.app.service('workers').remove(null, { 
     query: { userId: user.userId } 
     }); 
    } 
    } 
}); 
:関連エントリを削除

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

(例えば、ユーザーが削除された後、そのユーザのために、すべての労働者を除く)afterhookで行うことができます

関連する問題