首页 > 解决方案 > MongoDb 文档与内部对象连接

问题描述

我有 2 个名为 call 和客户的文档,我想将 locationId 加入到customers.location._id 的调用中

来电

      "_id": "5d456560221b9147e4af4b97",
      "customerId": "5d0a3bbfac9470aea56ad619",
      "locationId": "5d11d2c4cba8151f67f735b1",
      "startDateTime": "2019-06-21T05:30:00.000Z",
      "endDateTime": "2019-06-21T06:00:00.000Z"
    }

顾客

      "_id": "5d0a3bbfac9470aea56ad619",
      "locations": [
        {
          "_id": "5d11d2c4cba8151f67f735b1",
          "phone": "9889599999",
          "ext": "91",
          "address": "sad",
          "neighborhood": "sda",
          "city": "punetest",
          "state": "MH",
          "zip": "589365",
          "country": "india",
          "isBillingAddress": false,
          "taxPercentage": 0,
          "type": "Residential",
          "status": "Active",
          "firstName": "A",
          "lastName": "B"
        },
        {
          "_id": "5d11e457cba8151f67f735b3",
          "phone": "969696999",
          "ext": "91",
          "address": "",
          "neighborhood": "",
          "city": "udaipur",
          "state": "raj",
          "zip": "312563",
          "country": "india",
          "isBillingAddress": false,
          "taxPercentage": 0,
          "type": "Residential",
          "status": "Deleted",
          "firstName": "AB",
          "lastName": "CD"
        }
      ],
      "createdAt": "2019-06-19T13:42:23.479Z",
      "updatedAt": "2019-06-25T13:39:07.597Z"
    }
    [
     {
        $lookup: {
          from: 'customers.locations',
          localField: 'locationId',
          foreignField: '_id',
          as: 'customers.locations',
        },
      }
     ]);

它不起作用我有 2 个名为 call 和客户的文档,我想将 locationId 的呼叫加入到 customers.location._id

我想要输出``` { "_id": "5d456560221b9147e4af4b97", "customerId": "5d0a3bbfac9470aea56ad619", "locationId": "5d11d2c4cba8151f67f735b1", "location":{"_id": "5d11d2c4cba8151f69788""phone" ", "ext": "91", "address": "sad", "neighborhood": "sda", "city": "punetest", "state": "MH", "zip": "589365", “国家”:“印度”,“isBillingAddress”:假,“taxPercentage”:0,“类型”:“住宅”,“状态”:“活动”,“名字”:“A”,“姓氏”:“B”}}



标签: mongodb

解决方案


好的,这就是您正在查看的内容:

如果您locationId来电中只有一个匹配customer.locations._id客户

 db.getCollection('calls').aggregate([
  {
    $lookup: {
      from: 'customers',
      localField: 'locationId',
      foreignField: 'locations._id',
      as: 'customersCalls',
    }
  }, { $unwind: '$customersCalls' },
  {
    $project: {
      customerId: 1, locationId: 1, locations: {
        $filter: {
          input: "$customersCalls.locations",
          as: "item",
          cond: { $eq: ["$$item._id", '$locationId'] }
        }
      }
    }
  },
  { $group: { _id: '$_id', result: { $first: '$$ROOT' } } },
  { $replaceRoot: { newRoot: "$result" } }
])

否则,如果您的locationIdin呼叫customer.locations._idcustomer中有多个匹配项:

db.getCollection('calls').aggregate([
  {
    $lookup: {
      from: 'customers',
      localField: 'locationId',
      foreignField: 'locations._id',
      as: 'customersCalls',
    }
  }, { $unwind: '$customersCalls' },
  {
    $project: {
      customerId: 1, locationId: 1, locations: {
        $filter: {
          input: "$customersCalls.locations",
          as: "item",
          cond: { $eq: ["$$item._id", '$locationId'] }
        }
      }
    }
  },
  { $group: { _id: '$_id', locations: { $push: { $arrayElemAt: ["$locations", 0] } }, result: { $first: '$$ROOT' } } },
  { $addFields: { 'result.locations': '$locations' } },
  { $replaceRoot: { newRoot: "$result" } }
])

推荐阅读