【问题标题】:Mongodb aggregate lookup for many collections to one nested outputMongodb 将多个集合的查找聚合到一个嵌套输出
【发布时间】:2018-10-14 11:46:57
【问题描述】:

我有 3 个收藏:

学校

{   "id" : { "$numberLong" : "100000" },         
    "name" : "School1" }

教师

{   "id" : { "$numberLong" : "100000" }, 
    "schoolId" : { "$numberLong" : "100000" }, 
    "name" : "Faculty1" }

主题

{   "id" : { "$numberLong" : "100000" },       
    "name" : "Subject1" }

假设每个集合中有很多这样的。我希望能够提供一个接收 ID 并返回完整的 3 层层次结构(School->Faculty->Subject)的端点。我将如何返回所有这些数据。

类似:

{
    id: 1,
    name: "school1", 
    faculties: [{
        id:1000, 
        name: "faculty1", 
        subjects: [
            {id: 1, name: "sub1"},
            {id: 2, name: "sub2"},
            {id: 3, name: "sub3"}
        ]
    }]
}

【问题讨论】:

标签: mongodb mongodb-query aggregation-framework


【解决方案1】:

好吧,经过多年的努力,我实际上得到了解决方案,这比我掉下的兔子洞要简单得多。

{ $match: {id: 100001}},
{ $lookup:
    {
        from: 'faculties',
        localField: 'id',
        foreignField: 'schoolId',
        as: 'faculties',
    }
},
{ $unwind: {
    path: "$faculties",
    preserveNullAndEmptyArrays: true
  }
},
{ $lookup:
    {
        from: 'subjects',
        localField: 'faculties.id',
        foreignField: 'facultyId',
        as: 'faculties.subjects',
    }
}  

返回我想要的确切输出。关键是返回as: 'faculties.subjects' 的最终查找,它将科目放入学院内,这是学校的第一个孩子。

如果您需要进一步嵌套,则只需在每次深入时都去as: faculties.subjects.students.names

【讨论】:

    【解决方案2】:
    db.School.aggregate(
    
        // Pipeline
        [
            // Stage 1
            {
                $lookup: // Equality Match
                {
                    from: "Faculty",
                    localField: "id",
                    foreignField: "schoolId",
                    as: "faculties"
                }
    
    
            },
    
            // Stage 2
            {
                $unwind: {
                    path: "$faculties",
                    preserveNullAndEmptyArrays: false // optional
                }
            },
    
            // Stage 3
            {
                $lookup: // Equality Match
                {
                    from: "Subject",
                    localField: "faculties.id",
                    foreignField: "facultyId",
                    as: "faculties.subjects"
                }
    
    
            },
    
            // Stage 4
            {
                $group: {
                    _id: {
                        id: '$id',
                        name: '$name'
                    },
                    faculties: {
                        $addToSet: '$faculties'
                    }
                }
            },
    
            // Stage 5
            {
                $project: {
                    id: '$_id.id',
                    name: '$_id.name',
                    faculties: 1
                }
            },
    
        ]
    
    
    
    );
    

    【讨论】:

      猜你喜欢
      • 2020-02-16
      • 1970-01-01
      • 2013-06-17
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 2014-09-29
      相关资源
      最近更新 更多