【问题标题】:Mongodb aggregate three collectionsMongoDB聚合三个集合
【发布时间】:2018-03-06 07:26:17
【问题描述】:

这两天学习MongoDB,正在尝试聚合三个集合但无法实现

以下是数据库中维护的四个集合

大学

{
   "_id" : "5834ecf7432d92675bde9d82",
   "name": "NIFT"
}

大学

{
   "_id" : "5834ecf7432d92675bde9d83",
   "name": "NIFT Hyderabad",
   "university_id":"5834ecf7432d92675bde9d82"
}

部门

{
   "_id" : "5834ecf7432d92675bde9d84",
   "department_name": "Fashion Technology",
   "college_id" : "5834ecf7432d92675bde9d83" 
},
{
   "_id" : "5834ecf7432d92675bde9d85",
   "department_name": "Merchandising",
   "college_id" : "5834ecf7432d92675bde9d83"
}

部分

{
   "_id" : "5834ecf7432d92675bde9d86",
   "section_name": "A",
   "students" : "56",
   "department_id":"5834ecf7432d92675bde9d84"
},
{
   "_id" : "5834ecf7432d92675bde9d87",
   "section_name": "B",
   "students" : "60",
   "department_id":"5834ecf7432d92675bde9d84"
},
{
   "_id" : "5834ecf7432d92675bde9d86",
   "section_name": "A",
   "students" : "55",
   "department_id":"5834ecf7432d92675bde9d85"
},
{
   "_id" : "5834ecf7432d92675bde9d87",
   "section_name": "B",
   "students" : "44",
   "department_id":"5834ecf7432d92675bde9d85"
}

这里我试图实现以下格式的输出

预期输出

[{
   "_id": "5834ecf7432d92675bde9d83",
   "name": "NIFT Hyderabad",
   "university_id": "5834ecf7432d92675bde9d82",
   "departments": [{
        "_id": "5834ecf7432d92675bde9d84",
        "department_name": "CSE",
        "college_id": "5834ecf7432d92675bde9d83",
        "sections": [{
            "_id": "5834ecf7432d92675bde9d86",
            "section_name": "A",
            "students": "56",
            "department_id": "5834ecf7432d92675bde9d84"
        }, {
            "_id": "5834ecf7432d92675bde9d87",
            "section_name": "B",
            "students": "60",
            "department_id": "5834ecf7432d92675bde9d84"
        }]
    },
    {
        "_id": "5834ecf7432d92675bde9d85",
        "department_name": "Mechanical",
        "college_id": "5834ecf7432d92675bde9d83",
        "sections": [{
                "_id": "5834ecf7432d92675bde9d86",
                "section_name": "A",
                "students": "55",
                "department_id": "5834ecf7432d92675bde9d85"
            },
            {
                "_id": "5834ecf7432d92675bde9d87",
                "section_name": "B",
                "students": "44",
                "department_id": "5834ecf7432d92675bde9d85"
            }
        ]
    }
  ]
}]

但是,我在大学的单独数组中获取部门和部分,但无法获得上述格式

查询

db.college.aggregate([
            {"$match": { "university_id": "5834ecf7432d92675bde9d82" } },
            {"$lookup": {
            "localField": "_id",
            "from": "departments",
            "foreignField": "college_id",
            "as": "departments"
        }},
        {"$unwind":"$departments"},
        {$group : {_id : "$_id", departments : {$push : "$departments" }}},
        {"$lookup": {
        "localField": "departments._id",
        "from": "sections",
        "foreignField": "department_id",
        "as": "sections"}
        }
        ])

谁能帮我解决这个问题,对我很有帮助。

【问题讨论】:

    标签: mongodb nosql mongodb-query aggregation-framework


    【解决方案1】:

    您可以尝试以下聚合查询。

    下面的查询将sections 推送到department,当它们加入时,$group 推送部门以创建最终结构。

    db.college.aggregate([
      {
        "$match": {
          "university_id": "5834ecf7432d92675bde9d82"
        }
      },
      {
        "$lookup": {
          "localField": "_id",
          "from": "departments",
          "foreignField": "college_id",
          "as": "departments"
        }
      },
      {
       "$unwind": {
         "path": "$departments",
         "preserveNullAndEmptyArrays": true
        }
      },
      {
        "$lookup": {
          "localField": "departments._id",
          "from": "sections",
          "foreignField": "department_id",
          "as": "departments.sections"
        }
      },
      {
        "$group": {
          "_id": "$_id",
          "name": {
            "$first": "$name"
          },
          "university_id": {
            "$first": "$university_id"
          },
          "departments": {
            "$push": "$departments"
          }
        }
      }
    ])
    

    【讨论】:

    • 感谢@Veeram 它有效。我还面临一个问题,大学中有一个条目,该学院没有部门和部门条目,在这种情况下,无法在上述查询中单独获得该学院的条目。只有大学有部门条目,我才能得到
    • { "$unwind": { "path": "$departments", "preserveNullAndEmptyArrays": true } },我已经更新了展开以匹配空并且它有效,@Veeram 请更新相同感谢您的宝贵时间,这真的很有帮助并且学到了新的东西
    • 如果你想从大学而不是大学开始呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 2013-06-17
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    相关资源
    最近更新 更多