mongodb查询聚集Map多对多

sxissh06  于 2023-01-30  发布在  Go
关注(0)|答案(1)|浏览(153)

我有两个系列:
1.课程
1.学生
我想获取分配到相关课程对象中课程的学生的详细信息。
这是我的收藏和数据

课程:

[
    {
        _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()
        })
    }
})

有谁能给我推荐一个更好的单一聚合查询解决方案吗?

jm81lzqq

jm81lzqq1#

首先是$unwindstudentGroups,然后是students. Finally, regroup the results using $组'中的$lookup

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

Mongo Playground

相关问题