MongoDB(36)如何使用聚合进行分组?

在MongoDB中,使用聚合管道进行分组操作的关键阶段是 $group。该阶段允许你根据一个或多个字段将文档分组,并可以在每个组上进行各种计算,比如求和、求平均值、计数等。

使用 $group 阶段进行分组

$group 阶段的基本结构如下:

javascript 复制代码
{ 
  $group: {
    _id: <group key>, // 分组字段
    <field1>: { <accumulator1>: <expression1> },
    <field2>: { <accumulator2>: <expression2> },
    // 其他字段和计算
  } 
}

常见的累加器(accumulators)包括:

  • $sum:计算总和
  • $avg:计算平均值
  • $min:计算最小值
  • $max:计算最大值
  • $push:将值添加到数组
  • $addToSet:将唯一值添加到数组
  • $first:获取第一个值
  • $last:获取最后一个值

代码示例

以下是使用Node.js进行分组操作的详细示例。首先确保安装了MongoDB的Node.js驱动:

bash 复制代码
npm install mongodb
插入示例数据
javascript 复制代码
const { MongoClient } = require('mongodb');

async function insertData() {
    const uri = "mongodb://localhost:27017";
    const client = new MongoClient(uri, { useUnifiedTopology: true });

    try {
        await client.connect();
        const db = client.db('myDatabase');
        const collection = db.collection('orders');

        await collection.deleteMany({}); // 清空集合

        await collection.insertMany([
            { customerId: 1, amount: 100, status: "shipped" },
            { customerId: 1, amount: 200, status: "pending" },
            { customerId: 2, amount: 150, status: "shipped" },
            { customerId: 2, amount: 50, status: "pending" },
            { customerId: 3, amount: 250, status: "shipped" }
        ]);

        console.log("Data inserted");
    } finally {
        await client.close();
    }
}

insertData().catch(console.error);
使用聚合管道进行分组
javascript 复制代码
async function aggregateData() {
    const uri = "mongodb://localhost:27017";
    const client = new MongoClient(uri, { useUnifiedTopology: true });

    try {
        await client.connect();
        const db = client.db('myDatabase');
        const collection = db.collection('orders');

        // 使用 $group 进行分组
        console.log("\n$group stage:");
        let result = await collection.aggregate([
            { $group: { 
                _id: "$customerId", 
                totalAmount: { $sum: "$amount" }, // 计算每个客户的总金额
                avgAmount: { $avg: "$amount" },   // 计算每个客户的平均订单金额
                orderCount: { $sum: 1 },          // 计算每个客户的订单数量
                orders: { $push: "$$ROOT" }       // 将每个客户的订单放入数组
            }}
        ]).toArray();
        console.log(result);

    } finally {
        await client.close();
    }
}

aggregateData().catch(console.error);

在这个示例中,我们演示了如何使用 $group 阶段进行分组操作:

  1. 分组键 :$customerId,即我们按 customerId 字段分组。
  2. 计算总金额 :totalAmount,使用 $sum 计算每个客户的总订单金额。
  3. 计算平均订单金额 :avgAmount,使用 $avg 计算每个客户的平均订单金额。
  4. 计算订单数量 :orderCount,使用 $sum 并传入值 1 计算每个客户的订单数量。
  5. 收集订单 :orders,使用 $push 将每个客户的所有订单放入一个数组。

运行这个脚本后,你会得到如下结果(示例输出):

javascript 复制代码
[
  {
    "_id": 1,
    "totalAmount": 300,
    "avgAmount": 150,
    "orderCount": 2,
    "orders": [
      { "customerId": 1, "amount": 100, "status": "shipped" },
      { "customerId": 1, "amount": 200, "status": "pending" }
    ]
  },
  {
    "_id": 2,
    "totalAmount": 200,
    "avgAmount": 100,
    "orderCount": 2,
    "orders": [
      { "customerId": 2, "amount": 150, "status": "shipped" },
      { "customerId": 2, "amount": 50, "status": "pending" }
    ]
  },
  {
    "_id": 3,
    "totalAmount": 250,
    "avgAmount": 250,
    "orderCount": 1,
    "orders": [
      { "customerId": 3, "amount": 250, "status": "shipped" }
    ]
  }
]

其他语言示例

类似的分组操作也可以在其他编程语言中实现,如Python。以下是Python的示例代码:

安装PyMongo

在终端中运行以下命令来安装PyMongo:

bash 复制代码
pip install pymongo
使用Python进行分组
python 复制代码
from pymongo import MongoClient

def main():
    client = MongoClient('mongodb://localhost:27017/')
    db = client['myDatabase']
    collection = db['orders']

    # 使用 $group 进行分组
    pipeline = [
        { '$group': { 
            '_id': '$customerId', 
            'totalAmount': { '$sum': '$amount' }, 
            'avgAmount': { '$avg': '$amount' }, 
            'orderCount': { '$sum': 1 }, 
            'orders': { '$push': '$$ROOT' } 
        }}
    ]

    result = list(collection.aggregate(pipeline))
    print(result)

if __name__ == '__main__':
    main()

运行这个脚本后,你会得到类似的结果。通过这些示例,你可以了解到如何在不同编程语言中使用MongoDB的聚合管道进行分组操作,并在每个组上执行各种计算。

相关推荐
stormsha26 分钟前
MySQL 起不来只报 `Failed to set datadir`:真凶是 my.ini 里的四个字符
数据库·后端·mysql
许彰午30 分钟前
96-一个SQL请求的完整生命周期
数据库·oracle
Data-Miner34 分钟前
身份信息敏感数据分析:敏感度分级、数据落点与处理逻辑固化
数据库·人工智能·数据分析
北城笑笑36 分钟前
Vue 115 ,上篇:给智能体装上计费表,行知订阅次数额度与每日额度双规则设计( 需求分析与方案设计,Vue3 + RuoYi 3.9.2 + 主子账号 )
java·前端·javascript·数据库·vue.js·后端·vue
GuWenyue1 小时前
订单号末尾变成了 0,对账还少了 1 分钱:一个 53 位尾数惹的祸
javascript·数据库·后端
AI你一生一世1 小时前
集中式与分布式怎么选?谈谈金仓数据库的架构取舍
数据库·分布式·架构·分布式架构·数据库选型·金仓数据库·集中式架构
数据世界11 小时前
医院、零售、电商三端销售数据如何放进同一体系?分层抽样放大模型的分立与统一
大数据·数据库·人工智能
姚远Oracle ACE1 小时前
如何根据工作负载调优 PostgreSQL work_mem
数据库·postgresql
海宇大数据2 小时前
零信任架构实战:基于海宇对外投资历史查询服务构建自动化企业图谱投资聚合网关
数据库·人工智能·架构·自动化
冰暮流星2 小时前
mysql之触发器
android·数据库·mysql