MST

星途 面试题库

面试题:MongoDB中$unwind阶段如何拆分单个数组字段

假设有一个集合students,其中每个文档包含一个数组字段scores,表示学生的各项成绩。请写出使用$unwind阶段将scores数组拆分的聚合查询语句,并且返回拆分后每个成绩对应的学生姓名。文档结构如下:{name: '张三', scores: [85, 90, 78]}。
50.3万 热度难度
数据库MongoDB

知识考点

AI 面试

面试题答案

一键面试
db.students.aggregate([
    {
        $unwind: "$scores"
    },
    {
        $project: {
            name: 1,
            score: "$scores"
        }
    }
]);