简体   繁体   English

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

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

I'm trying to remove document in two different collection using same id at same time. 我正在尝试同时使用相同的ID删除两个不同集合中的文档。 Is there any possibilities? 有没有可能?

user collection: 用户集合:

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

worker collection: 工人集合:

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

now i want to remove these two document at same time using userId. 现在,我想使用userId同时删除这两个文档。

Database adapters allow to call remove with null and a query that will remove all entries matching that query (see the documentation ). 数据库适配器允许调用带有null remove和一个查询,该查询将删除与该查询匹配的所有条目(请参阅文档 )。 In your case: 在您的情况下:

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

Removing related entries (eg remove all workers for that user once the user has been removed) can be done in an after hook : 删除相关条目(例如,在删除用户后删除该用户的所有工作程序)可以在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.

相关问题 如何在MongoDB的集合中找到两个时间字段之间的差异并将结果添加到同一文档中? - How can I find the difference between two time fields and add the result in the same document, inside a collection at MongoDB? 如何将一个文档保存到 mongodb 中同一数据库的两个不同 collections 中 - How to save one document into two different collections of same database in mongodb 如何使用文档ID从mongoDB集合中删除文档? - How to delete document from mongoDB collection using the document's ID? 如何删除具有相同ID的两个json对象 - how to remove two json objects with same id 如何使用PHP或JavaScript在两个不同的打印机上同时打印? - How to print in two different printers at the same time using PHP or JavaScript? 如何使用同一数据库中另一个集合的数据创建 mongoDB 集合 - how create mongoDB collection using data of another collection in same database 滚动到同一文档中两个不同列表中具有相同 id 值的元素 - Scroll to elements from two different lists in the same document that have the same id value 如何通过使用同一按钮使用文本框的两个ID来执行两种不同的操作 - How to perform two different action by using two ID's of textbox using one same button 使用不同的端口同时运行两个节点应用程序 - run two node application at same time with different port 如何在Ember.js应用程序中同时使用两个不同的适配器? - How to use two different adapters at the same time in Ember.js application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM