MST

星途 面试题库

面试题:MongoDB聚合管道结果写入集合:基本操作

在MongoDB中,假设已有一个集合`orders`,其中包含订单信息,如订单金额`amount`、下单时间`orderTime`等字段。请写出聚合管道语句,统计每个月的订单总金额,并将结果写入到一个新集合`monthly_order_amount`中。
49.0万 热度难度
数据库MongoDB

知识考点

AI 面试

面试题答案

一键面试
db.orders.aggregate([
    {
        $group: {
            _id: { $dateToString: { format: "%Y-%m", date: "$orderTime" } },
            totalAmount: { $sum: "$amount" }
        }
    },
    {
        $out: "monthly_order_amount"
    }
])