繁体   English   中英

mongodb 查询聚合 map 多对多

[英]mongodb query agregate map many to many

我有两个 collections:

  1. 培训班
  2. 学生们

我想获取分配给相关课程 object 中课程的学生的详细信息。

例如,这是我的 collections 和数据

培训班:

[
    {
        _id: 1,
        title: "Maths",
        studentGroups: [
            {
                group: 'A',
                students: [1, 5]

            },
            {
                group: 'B',
                students: [3]

            }
        ]
    },
    {
        _id: 2,
        title: "Chemistry",
        studentGroups: [
            {
                group: 'C',
                students: [2]

            },
            {
                group: 'B',
                students: [4]

            }
        ]
    }
]

学生们:

[
    {
        _id:  1,
        name: 'Henry',
        age: 15
    },
    {
        _id:  2,
        name: 'Kim',
        age: 20
    },
    {
        _id:  3,
        name: 'Michel',
        age: 14
    },
    {
        _id:  4,
        name: 'Max',
        age: 16
    },
    {
        _id:  5,
        name: 'Nathan',
        age: 19
    }
]

现在我想要这个回应:

[
    {
        _id: 1,
        title: "Maths",
        studentGroups: [
            {
                group: 'A',
                students: [
                    {
                        _id:  1,
                        name: 'Henry',
                        age: 15
                    },
                    {
                        _id:  5,
                        name: 'Nathan',
                        age: 19
                    }
                ]

            },
            {
                group: 'B',
                students: [
                    {
                        _id:  3,
                        name: 'Michel',
                        age: 14
                    }
                ]

            }
        ]
    },
    {
        _id: 2,
        title: "Chemistry",
        studentGroups: [
            {
                group: 'C',
                students: [
                    {
                        _id:  2,
                        name: 'Kim',
                        age: 20
                    }
                ]

            },
            {
                group: 'B',
                students: [
                    {
                        _id:  4,
                        name: 'Max',
                        age: 16
                    }
                ]
            }
        ]
    }
]

我尝试的结果给出了正确的结果,但这不是一个好方法,我需要通过单个聚合查询来实现

我的解决方案:

var courses = Courses.find({}).fetch();

courses.forEach(c => {
    if (c.studentGroups && c.studentGroups.length) {
        c.studentGroups.forEach(s => {
            s.students = Students.find({_id: {$in: s.students}}).fetch()
        })
    }
})

谁能建议我使用单一聚合查询的更好解决方案?

首先$unwind the studentGroups 然后, $lookup来自students. Finally, regroup the results using students. Finally, regroup the results using

db.courses.aggregate([
  {
    "$unwind": "$studentGroups"
  },
  {
    "$lookup": {
      "from": "students",
      "localField": "studentGroups.students",
      "foreignField": "_id",
      "as": "studentGroups.students"
    }
  },
  {
    $group: {
      _id: "$_id",
      title: {
        $first: "$title"
      },
      studentGroups: {
        $push: "$studentGroups"
      },
      description: {
        $first: "$description"
      },
      timeline: {
        $first: "$timeline"
      }
    }
  }
])

蒙戈游乐场

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM