MST

星途 面试题库

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

假设有一个集合名为 'orders',包含订单信息,每个文档有 'customer'(客户名)、'order_date'(订单日期)和 'total_amount'(订单总金额)字段。请使用MongoDB管道操作,统计每个客户的订单总金额之和,并按总金额从高到低排序。
38.3万 热度难度
数据库MongoDB

知识考点

AI 面试

面试题答案

一键面试
db.orders.aggregate([
    {
        $group: {
            _id: "$customer",
            totalAmount: { $sum: "$total_amount" }
        }
    },
    {
        $sort: {
            totalAmount: -1
        }
    }
]);