【问题标题】:MongoDb documents join with inner objectsMongoDb 文档与内部对象连接
【发布时间】:2019-08-13 08:16:14
【问题描述】:

我有 2 个名为 call 和 customers 的文档 我想加入对customers.location._id的呼叫的locationId

通话

      "_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", “位置”:{“_id”:“5d11d2c4cba8151f67f735b1”, “电话”:“9889599999”, “分机”:“91”, “地址”:“悲伤”, “邻居”:“sda”, "城市": "punetest", “状态”:“MH”, “邮编”:“589365”, “国家”:“印度”, “isBillingAddress”:假, “税收百分比”:0, “类型”:“住宅”, “状态”:“活动”, “名字”:“A”, “姓氏”:“B”} }



【问题讨论】:

    标签: mongodb


    【解决方案1】:

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

    如果您在calls中的locationIdcustomer中只有一个匹配的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" } }
    ])
    

    如果您在calls中的locationIdcustomer中有多个匹配的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', locations: { $push: { $arrayElemAt: ["$locations", 0] } }, result: { $first: '$$ROOT' } } },
      { $addFields: { 'result.locations': '$locations' } },
      { $replaceRoot: { newRoot: "$result" } }
    ])
    

    【讨论】:

    • 我正在接听电话..加入客户的电话..我尝试了上述方法,但它不起作用..
    • @vimmi:现在有什么问题?您只想要位置数组中的匹配文档吗?是这个问题吗?
    • 它正在加入客户文件,我想加入客户文件中的位置。现在你明白我的意思了吗?
    • @vimmi :加入客户文档是什么意思?它与locations._id映射,我在这里看到的唯一问题是locations中的多个文档(这是因为如果一个元素与数组上的条件匹配,它将返回整个数组而不是匹配的元素),如果你不需要也许您需要通过 $unwind locations 数组对客户编写条件或执行此操作,如果不是这种情况,那么 $lookup 就是 !!
    • 我在客户文档中有多个位置.. 有什么解决方案可以完成这项工作吗?或者我应该在不同的文档中移动位置
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多