MST

星途 面试题库

面试题:MongoDB聚合管道之基本操作

假设有一个名为`students`的集合,文档结构包含`name`(字符串)、`age`(数字)、`scores`(数组,存储各科成绩)。请使用MongoDB聚合管道查询出年龄大于18岁且平均成绩大于80分的学生姓名,需要展示具体的聚合管道语句。
17.8万 热度难度
数据库MongoDB

知识考点

AI 面试

面试题答案

一键面试
[
    {
        $match: {
            age: { $gt: 18 }
        }
    },
    {
        $addFields: {
            averageScore: { $avg: "$scores" }
        }
    },
    {
        $match: {
            averageScore: { $gt: 80 }
        }
    },
    {
        $project: {
            name: 1,
            _id: 0
        }
    }
]