0

我有一个包含一组模块的课程集,每个模块都有一组主题,每个主题都有一组单元。

课程合集:

{
    "course": {
        "_id": "612f5605dc3813a3717663fe",
        "cmodules": [
            "612f5612dc3813068b76646c"
        "612f561adc3813e7a27664f6",
        ],
        "title": "C++ Complete Course"
    }
}

模块合集:

{
    "cmodules": [
        {
            "_id": "612f561adc3813e7a27664f6",
            "topics": [
                "612f561bdc381332647664fd"
                "612f5615dc3813d8a47664bb",
                "612f5617dc38130e017664de"
            ],
            "title": "Welcome to C++",
            "desc": "C++ Introduction",
        },
        {
            "_id": "612f5612dc3813068b76646c",
            "topics": [
                "612f5612dc3813ef8c766473"
                "612f561cdc38132d73766514",
                "612f561fdc3813ad1676652d"
            ],
            "title": "Introduction to STL",
            "desc": "Learn Standard template library",
        }
    ]
}

话题合集:

{
    "topics": [
        {
            "_id": "612f561bdc381332647664fd",
            "units": [
                "612f561bdc3813154b766503",
                "612f561cdc3813feba76650b"
            ],
            "order": 0,
            "title": "Session 1",
        },
        {
            "_id": "612f561cdc38132d73766514",
            "units": [
                "612f561ddc381317b176651a",
                "612f561ddc38135e3a766520",
                "612f561edc3813bf45766526"
            ],
            "title": "Session 2",
        },
        {
            "_id": "612f561fdc3813ad1676652d",
            "units": [
                "612f5620dc3813491b766533",
                "612f5620dc3813b5f2766539",
                "612f5621dc381387d976653f"
            ],
            "title": "Session 3",
        }
    ]
}

learnedUnits在用户详细信息的数组()中跟踪学生访问的所有单元 ID 列表。

我做了一些聚合管道和展开收集,直到模块。看看我的聚合结果。

[
    {
        "_id": "5fd5c105694d122f4837fa0f",
        "learningPath": {
            "historyCourses": {
                "_id": "612f5605dc3813a3717663fe",
                "startedAt": "2021-09-03T13:58:32.071Z",
                "learnedUnits": [
                    "612f5608dc38133ee6766411",
                    "61322aa16c985ae927308a82",
                    "612f5608dc3813268a766417",
                    "612f5609dc38137cd776641d"
                ],
                "course": {
                    "_id": "612f5605dc3813a3717663fe",
                    "cmodules": {
                        "_id": "612f5612dc3813068b76646c",
                        "topics": [
                            {
                                "_id": "612f5612dc3813ef8c766473",
                                "units": [
                                    "612f5613dc381382797664ac",
                                    "612f5614dc38139a927664b2"
                                ],
                                "title": "Session 1",
                            },
                            {
                                "_id": "612f5615dc3813d8a47664bb",
                                "units": [
                                    "612f5615dc38131ee17664c8",
                                    "612f5616dc381317fa7664ce",
                                    "612f5616dc38132ac37664d5"
                                ],
                                "title": "Session 2",
                            },
                            {
                                "_id": "612f5617dc38130e017664de",
                                "units": [
                                    "612f5618dc381399cf7664e4",
                                    "612f5618dc38138e367664ea",
                                    "612f5619dc38137fe87664f0"
                                ],
                                "title": "Session 3",
                            }
                        ],
                        "title": "Welcome to C++",
                        "desc": "C++ Introduction",
                    },
                    "title": "C++ Complete Course",
                }
            }
        }
    },
    {
        "_id": "5fd5c105694d122f4837fa0f",
        "learningPath": {
            "historyCourses": {
                "_id": "612f5605dc3813a3717663fe",
                "startedAt": "2021-09-03T13:58:32.071Z",
                "learnedUnits": [
                    "612f5608dc38133ee6766411",
                    "61322aa16c985ae927308a82",
                    "612f5608dc3813268a766417",
                    "612f5609dc38137cd776641d"
                ],
                "course": {
                    "_id": "612f5605dc3813a3717663fe",
                    "cmodules": {
                        "_id": "612f561adc3813e7a27664f6",
                        "topics": [
                            {
                                "_id": "612f561bdc381332647664fd",
                                "units": [
                                    "612f561bdc3813154b766503",
                                    "612f561cdc3813feba76650b"
                                ],
                                "order": 0,
                                "title": "Session 1",
                            },
                            {
                                "_id": "612f561cdc38132d73766514",
                                "units": [
                                    "612f561ddc381317b176651a",
                                    "612f561ddc38135e3a766520",
                                    "612f561edc3813bf45766526"
                                ],
                                "title": "Session 2",
                            },
                            {
                                "_id": "612f561fdc3813ad1676652d",
                                "units": [
                                    "612f5620dc3813491b766533",
                                    "612f5620dc3813b5f2766539",
                                    "612f5621dc381387d976653f"
                                ],
                                "title": "Session 3",
                            }
                        ],
                        "title": "Introduction to STL",
                        "desc": "Learn Standard template library",
                    },
                    "title": "C++ Complete Course",
                }
            }
        }
    }
]

我想在这里做两件事。首先,我想按课程 ID 对所有聚合集合进行分组。模块应该作为课程的数组来收集。有些人认为如下。

{
    "_id": "5fd5c105694d122f4837fa0f",
    "learningPath": {
        "historyCourses": {
            "_id": "612f5605dc3813a3717663fe",
            "startedAt": "2021-09-03T13:58:32.071Z",
            "learnedUnits": [
                "612f5608dc38133ee6766411",
                "61322aa16c985ae927308a82",
                "612f5608dc3813268a766417",
                "612f5609dc38137cd776641d"
            ],
            "course": {
                "_id": "612f5605dc3813a3717663fe",
                "cmodules": [
                    {
                        "_id": "612f5612dc3813068b76646c",
                        "topics": [
                            {
                                "_id": "612f5612dc3813ef8c766473",
                                "units": [
                                    "612f5613dc381382797664ac",
                                    "612f5614dc38139a927664b2"
                                ]
                            }
                        ]
                    },
                    {
                        "_id": "612f561adc3813e7a27664f6",
                        "topics": [
                            {
                                "_id": "612f561bdc381332647664fd",
                                "units": [
                                    "612f561bdc3813154b766503",
                                    "612f561cdc3813feba76650b"
                                ]
                            }
                        ]
                    }
                ]
            }
        }
    }
}

其次,我想通过阅读learnedUnits课程单元来找到学生的平均完成百分比。在我上面的例子中,用户访问了 4 个单元。如果结合所有模块和主题的单元总数为 20,那么我想找到访问单元的平均值为 20%。

有没有办法在同一个聚合管道中做到这一点?

4

0 回答 0