随笔 - 2649  文章 - 2452  评论 - 0  阅读 - 74060

聚合例子

聚合例子

数据集

假设有一个名为 books 的图书集合,其示例数据结构如下:

{
  "_id": "xxxx-xxxx-xxxx",
  "category": "novel",
  "title": "title 1",
  "author": "author 1",
  "sales": 5000,
  "monthlySales": [1000, 1500, 2500]
}

例:求各类图书的平均销量

采用 books 图书数据集作为示例。求各类图书的平均销量的操作等价于将图书按类别(category)分组,然后对每组的销量(sales)求平均值,最后返回类别和平均销量信息。

const db = wx.cloud.database()
const $ = db.command.aggregate
const result = await db.collection('books').aggregate()
  .group({
    // 按 category 字段分组
    _id: '$category',
    // 每组有一个 avgSales 字段,其值是组内所有记录的 sales 字段的平均值
    avgSales: $.avg('$sales')
  })
  .end()

返回结果示例如下:

{
  "list": [
    {
      _id: "novel", // 组名
      sales: 1035, // 组平均销量
    },
    {
      _id: "science", // 组名
      sales: 350, // 组平均销量
    }
  ]
}

用到的聚合流水线阶段:group

用到的聚合流水线操作符:avg

例:求各类图书的最高销量作者和最低销量作者及其销量

采用 books 图书数据集作为示例。一个作者在一个图书类目下可能有多本书,这个操作可以分阶段进行批处理:

  • 第一阶段: 按 "图书类目 + 作者" 进行分组,求得每个作者在该图书类目下的多本书的总销量
  • 第二阶段: 将上一个阶段得到的结果按总销量排倒序,求得各图书类目作者的总销量排序
  • 第三阶段: 将上一个阶段得到的结果按类目分组,各组分别取组中第一项(销量最高)和最后一项(销量最低)的作者名和总销量,返回最终结果
const db = wx.cloud.database()
const $ = db.command.aggregate
const result = db.collection('books').aggregate()
  .group({
    _id: {
      category: '$category',
      author: '$author',
    },
    totalSales: $.sum('$sales')
  })
  .sort({
    totalSales: -1,
  })
  .group({
    _id: '$_id.category',
    bestSellingAuthor: $.first('$_id.author'),
    bestSellingAuthorTotalSales: $.first('$totalSales'),
    worstSellingAuthor: $.last('$_id.author'),
    worstSellingAuthorTotalSales: $.last('$totalSales'),
  })
  .end().then(console.log)

返回结果示例如下:

{
  "list": [
    {
      "_id": "novel",
      "bestSellingAuthor": "author 1",
      "bestSellingAuthorTotalSales": 550,
      "worstSellingAuthor": "author 2",
      "worstSellingAuthorTotalSales": 1520
    },
    {
      "_id": "science",
      "bestSellingAuthor": "author 4",
      "bestSellingAuthorTotalSales": 1050,
      "worstSellingAuthor": "author 3",
      "worstSellingAuthorTotalSales": 3000
    }
  ]
}

用到的聚合流水线阶段:groupsort

用到的聚合流水线操作符:sumfirstlast

posted on   AtlasLapetos  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示