【问题标题】:How to handle multiple $lookup and large number of columns in $project in mongoDB如何在 mongoDB 中处理 $project 中的多个 $lookup 和大量列
【发布时间】:2019-06-12 10:01:56
【问题描述】:

我需要加入几乎 4-5 个表。我已经使用$lookup 完成了这项工作,我也不需要辅助表/连接表中的所有字段。我需要主表的所有列。所以,我写了$project 来选择必要的列。

Room.aggregate([{
        $lookup: {
            from: "users",
            localField: "userID",
            foreignField: "_id",
            as: "user"
        }
    }, {
        $lookup: {
            from: "areas",
            localField: "area",
            foreignField: "_id",
            as: "areas"
        }
    }, {
        $unwind: "$user",

    }, {$project : {

    }  
    }]

我想知道是否可以无论如何简化此查询或在单独的位置创建并以更易读和更简单的格式合并到主查询中。

另外,我需要指定 $project 中的每一列,因为我想要 Room 表中的所有列,而连接表中只有几列。

更新

这是我的示例集合定义
房间:

{
    "_id": {
        "$oid": "5bcc2eb704c952178c4adbcd"
    },
    "userID": {
        "$oid": "5c0d4e9e7377833f3c362a63"
    },
    "roomQTY": 1,
    "roomPrice": 500,
    "area": [
        {
            "$oid": "5bd4a49857e0c023b0dac7b2"
        }
    ],
    "rating": 3,
    "amenities": [],
    "images": [],
    "isFurnished": true,
    "isActive": true,
    "category": {
        "$oid": "5c0c1438ccbc830d248167bf"
    },
    "roomName": "Shree Laxminarayan Residency",
    "person": "4",
    "size": 157,
    "createdDate": {
        "$date": "2018-10-21T07:45:59.492Z"
    },
    "updatedDate": {
        "$date": "2018-10-21T07:45:59.492Z"
    },
    "__v": 0
}

设施:

{
    "_id": {
        "$oid": "5bd4b19e57e0c023b0dac7cd"
    },
    "ammenitiesName": "Swimming Pool"
}

类别:

{
    "_id": {
        "$oid": "5c0c1438ccbc830d248167bf"
    },
    "category_name": "PG"
}

愿望输出:

{
  "data": [
    {
      "_id": "5bcc2eb704c952178c4adbcd",
      "userID": "5c0d4e9e7377833f3c362a63",
      "roomQTY": 1,
      "roomPrice": 500,
      "rating": 3,
      "amenities": [],
      "images": [],
      "isFurnished": true,
      "isActive": true,
      "category": "5c0c1438ccbc830d248167bf",
      "roomName": "Shree Laxminarayan Residency",
      "person": "4",
      "size": 157,
      "createdDate": "2018-10-21T07:45:59.492Z",
      "updatedDate": "2018-10-21T07:45:59.492Z",
      "username": "Hardik",
      "areas": [
        {
          "_id": "5bd4a49857e0c023b0dac7b2",
          "areaName": "South Bopal"
        }
      ],
      "ammenities": [
        {
          "ammentiesName": "Swimming Pool"
        },
        {
          "ammentiesName": "Gym"
        }
      ]
    }
  ],
  "code": 200
}

【问题讨论】:

  • 发布您的样本集合和您需要的输出
  • 好的。一定要给我一点时间

标签: mongodb join aggregate


【解决方案1】:

Aggregation 在 MongoDB 3.6 或更高版本中提供加入集合的管道。

db.rooms.aggregate([{
    $lookup: {
      from: "Ammenities",
      let: {
        amenitiesIds: "$amenities"
      },
      pipeline: [{
          $match: {
            $expr: { $in: [ "$_id", "$$amenitiesIds" ] }
          }
        },
        { $project: { _id: 0 } }
      ],
      as: "ammenities"
    }
  },
  {
    $lookup: {
      from: "Category",
      let: {
        categoryId: "$category"
      },
      pipeline: [{
          $match: {
            $expr: { $eq: [ "$_id", "$$categoryId" ] }
          }
        },
        { $project: { _id: 0 } }
      ],
      as: "category"
    }
  }
])

您可以在内部管道酷上添加多个管道。

运行它,你的结果在等着你...

【讨论】:

  • 当然我会检查管道。杰伊帕蒂达..杰伊萨达尔
猜你喜欢
  • 2019-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
  • 1970-01-01
  • 2017-10-08
  • 1970-01-01
相关资源
最近更新 更多