如何在mongoDB中的$ project中处理多个$ lookup和大量列

时间:2019-01-18 06:41:12

标签: mongodb join aggregate

我几乎有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
}

1 个答案:

答案 0 :(得分:1)

MongoDB 3.6或更高版本中的

Aggregation 提供连接集合上的管道。

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"
    }
  }
])

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

运行它,结果正在等待您...