MongoDB聚合:$bucketAuto

按照指定的表达式对输入文档进行分类后放入指定数字的桶中,跟$bucket不太一样,$bucketAuto可以指定分组的数量(颗粒度),$bucketAuto会根据groupBy的值和颗粒度自动生成桶的边界。

语法

js 复制代码
{
  $bucketAuto: {
      groupBy: <表达式>,
      buckets: <数字>,
      output: {
         <输出1>: { <$accumulator 表达式> },
         ...
      }
      granularity: <字符串>
  }
}

groupBy

表达式,对文档进行分组的表达式。若指定字段路径,需要在字段名前加上美元符号$并用引号引起来,如:$field_name

buckets

整数,32位的正整数,指定桶的数量也就是输入文档分组的数量。

output

文档,可选,指定了输出文档中除_id字段外要包含的其他字段,必须要使用汇总(累加器)表达式:

js 复制代码
<输出字段1>: { <accumulator>: <表达式1> },
...

如果指定了输出字段,则count字段不会自动添加,需要的话要手动添加。如果不指定输出字段则默认添加一个count字段。

js 复制代码
output: {
  <输出字段1>: { <accumulator>: <expres表达式1sion1> },
  ...
  count: { $sum: 1 }
}

每个桶文档包含:

  • 一个桶边界下限的_id
    • _id.min字段指定了桶边界的下限(含)。
    • _id.max字段指定了桶边界的上限(不含)。除系列中的最后一个桶外,该界限对所有桶都是排他性的,因为在最后一个桶中,该界限是包含的。
  • count字段,包含文件桶中的文件数量。如果未指定输出文档,则默认包含count字段。

granularity

可选,字符串,指定了一个字符串,用于指定首选数列,以确保计算的边界边缘以首选的整数或其10的幂次结束。只有当所有groupBy值都是数值且都不是NaN时才有效。

支持的颗粒度:"R5","R10","R20","R40","R80","1-2-5","E6","E12","E24","E48","E96","E192","POWERSOF2"。

说明

如果出现以下情况,桶数量可能少于指定数量:

  • 输入文件的数量少于指定的文件桶数量。
  • groupBy表达式的唯一值数量少于指定的存储桶数量。
  • 粒度的间隔数少于桶数。
  • 粒度不够精细,无法将文档均匀分布到指定数量的桶中。

groupBy字段的粒度或唯一值的数量决定了文档是否能均匀分布到不同的桶。如果粒度不够,$bucketAuto阶段可能无法将结果均匀地分配到各个桶。

粒度

$bucketAuto接受一个可选的粒度参数,确保所有数据桶的边界都遵循指定的首选数列。使用首选数列可以更好地控制分组表达式中数值范围内的数据桶边界。当groupBy表达式的范围以指数形式扩展时,还可以使用首选数列帮助对数和均匀地设置数据桶边界。

雷纳数列

雷纳数列是通过取105次方根、10次方根、20次方根、40次方根或80次方根,然后将相当于1.0到 10.0(R80 为 10.3)之间数值的根的各种幂包含在内而得出的一组数字。

将粒度设置为 R5、R10、R20、R40 或 R80,可将数据桶边界限制为系列中的值。当 groupBy 值超出 1.0 至 10.0(R80 为 10.3)范围时,系列值将乘以 10 的幂。

R5 数列以 10 的五次方根 1.58 为基础,包括该根的各种幂次(四舍五入),直至 10。R5 数列的推导过程如下:

例如:

  • 10 0/5 = 1
  • 10 1/5 = 1.584 ~ 1.6
  • 10 2/5 = 2.511 ~ 2.5
  • 10 3/5 = 3.981 ~ 4.0
  • 10 4/5 = 6.309 ~ 6.3
  • 10 5/5 = 10

同样的方法也适用于其他雷纳系列,以提供更精细的粒度,即 1.0 和 10.0 之间的更多间隔(R80 为 10.3)。

E 序列

E 数字系列与雷纳数列类似,它们以特定的相对误差将 1.0 到 10.0 的区间细分为10的6、12、24、48、96或192的次方根。

将粒度设置为 E6、E12、E24、E48、E96 或 E192,可将桶边界限制为序列中的值。当 groupBy 值超出 1.0 到 10.0 的范围时,系列值将乘以 10 的幂。

1-2-5 序列

1-2-5 数列类似于三值数列雷纳数列。

将粒度设为 1-2-5,可将桶边界限制为 10 的三次根的各种幂,四舍五入到一位有效数字。

例如,以下数值属于 1-2-5 系列:0.1、0.2、0.5、1、2、5、10、20、50、100、200、500、1000 等...

2的次幂序列

将粒度设置为 POWERSOF2,限制桶边界为2的次幂

以下数字遵循2的幂序列:

  • 2^0 = 1
  • 2^1 = 2
  • 2^2 = 4
  • 2^3 = 8
  • 2^4 = 16
  • 2^5 = 32
  • ...

一种常见的实现方式是,各种计算机组件(如内存)通常都遵守POWERSOF2的首选数字集:1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, ...

不同粒度的比较

下面的操作演示了指定不同的粒度值如何影响$bucketAuto确定桶边界的方式。集合的_id从 0 到 99:

js 复制代码
{ _id: 0 }
{ _id: 1 }
...
{ _id: 99 }

不同的粒度值会被代入到下面的操作中:

js 复制代码
db.things.aggregate( [
  {
    $bucketAuto: {
      groupBy: "$_id",
      buckets: 5,
      granularity: <granularity>
    }
  }
] )

下表中的结果显示了不同的粒度值如何产生不同的桶边界:

粒度 结果 说明
无粒度 { "_id" : { "min" : 0, "max" : 20 }, "count" : 20 }{ "_id" : { "min" : 20, "max" : 40 }, "count" : 20 }{ "_id" : { "min" : 40, "max" : 60 }, "count" : 20 }{ "_id" : { "min" : 60, "max" : 80 }, "count" : 20 }{ "_id" : { "min" : 80, "max" : 99 }, "count" : 20 }
R20 { "_id" : { "min" : 0, "max" : 20 }, "count" : 20 }{ "_id" : { "min" : 20, "max" : 40 }, "count" : 20 }{ "_id" : { "min" : 40, "max" : 63 }, "count" : 23 }{ "_id" : { "min" : 63, "max" : 90 }, "count" : 27 }{ "_id" : { "min" : 90, "max" : 100 }, "count" : 10 }
E24 { "_id" : { "min" : 0, "max" : 20 }, "count" : 20 }{ "_id" : { "min" : 20, "max" : 43 }, "count" : 23 }{ "_id" : { "min" : 43, "max" : 68 }, "count" : 25 }{ "_id" : { "min" : 68, "max" : 91 }, "count" : 23 }{ "_id" : { "min" : 91, "max" : 100 }, "count" : 9 }
1-2-5 { "_id" : { "min" : 0, "max" : 20 }, "count" : 20 }{ "_id" : { "min" : 20, "max" : 50 }, "count" : 30 }{ "_id" : { "min" : 50, "max" : 100 }, "count" : 50 } 指定的桶数超过系列中的间隔数。
POWERSOF2 { "_id" : { "min" : 0, "max" : 32 }, "count" : 32 }{ "_id" : { "min" : 32, "max" : 64 }, "count" : 32 }{ "_id" : { "min" : 64, "max" : 128 }, "count" : 36 } 指定的桶数超过系列中的间隔数。

举例

下面的文档是收藏艺术品的集合:

json 复制代码
{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926,
    "price" : NumberDecimal("199.99"),
    "dimensions" : { "height" : 39, "width" : 21, "units" : "in" } }
{ "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902,
    "price" : NumberDecimal("280.00"),
    "dimensions" : { "height" : 49, "width" : 32, "units" : "in" } }
{ "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925,
    "price" : NumberDecimal("76.04"),
    "dimensions" : { "height" : 25, "width" : 20, "units" : "in" } }
{ "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai",
    "price" : NumberDecimal("167.30"),
    "dimensions" : { "height" : 24, "width" : 36, "units" : "in" } }
{ "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931,
    "price" : NumberDecimal("483.00"),
    "dimensions" : { "height" : 20, "width" : 24, "units" : "in" } }
{ "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913,
    "price" : NumberDecimal("385.00"),
    "dimensions" : { "height" : 30, "width" : 46, "units" : "in" } }
{ "_id" : 7, "title" : "The Scream", "artist" : "Munch",
    "price" : NumberDecimal("159.00"),
    "dimensions" : { "height" : 24, "width" : 18, "units" : "in" } }
{ "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918,
    "price" : NumberDecimal("118.42"),
    "dimensions" : { "height" : 24, "width" : 20, "units" : "in" } }

单面聚合

在下面的操作中,输入文档将根据price字段中的值分成四组:

js 复制代码
db.artwork.aggregate( [
   {
     $bucketAuto: {
         groupBy: "$price",
         buckets: 4
     }
   }
] )

该操作会返回以下文件:

json 复制代码
{
  "_id" : {
    "min" : NumberDecimal("76.04"),
    "max" : NumberDecimal("159.00")
  },
  "count" : 2
}
{
  "_id" : {
    "min" : NumberDecimal("159.00"),
    "max" : NumberDecimal("199.99")
  },
  "count" : 2
}
{
  "_id" : {
    "min" : NumberDecimal("199.99"),
    "max" : NumberDecimal("385.00")
  },
  "count" : 2
}
{
  "_id" : {
    "min" : NumberDecimal("385.00"),
    "max" : NumberDecimal("483.00")
  },
  "count" : 2
}

多面聚合

...。

可在$facet阶段内使用$bucketAuto,对输入文档artwork进行多个聚合管道处理。

下面的聚合管道根据priceyearareaartwork 中的文档分组:

js 复制代码
db.artwork.aggregate( [
  {
    $facet: {
      "price": [
        {
          $bucketAuto: {
            groupBy: "$price",
            buckets: 4
          }
        }
      ],
      "year": [
        {
          $bucketAuto: {
            groupBy: "$year",
            buckets: 3,
            output: {
              "count": { $sum: 1 },
              "years": { $push: "$year" }
            }
          }
        }
      ],
      "area": [
        {
          $bucketAuto: {
            groupBy: {
              $multiply: [ "$dimensions.height", "$dimensions.width" ]
            },
            buckets: 4,
            output: {
              "count": { $sum: 1 },
              "titles": { $push: "$title" }
            }
          }
        }
      ]
    }
  }
] )

操作返回以下内容:

json 复制代码
{
  "area" : [
    {
      "_id" : { "min" : 432, "max" : 500 },
      "count" : 3,
      "titles" : [
        "The Scream",
        "The Persistence of Memory",
        "Blue Flower"
      ]
    },
    {
      "_id" : { "min" : 500, "max" : 864 },
      "count" : 2,
      "titles" : [
        "Dancer",
        "The Pillars of Society"
      ]
    },
    {
      "_id" : { "min" : 864, "max" : 1568 },
      "count" : 2,
      "titles" : [
        "The Great Wave off Kanagawa",
        "Composition VII"
      ]
    },
    {
      "_id" : { "min" : 1568, "max" : 1568 },
      "count" : 1,
      "titles" : [
        "Melancholy III"
      ]
    }
  ],
  "price" : [
    {
      "_id" : { "min" : NumberDecimal("76.04"), "max" : NumberDecimal("159.00") },
      "count" : 2
    },
    {
      "_id" : { "min" : NumberDecimal("159.00"), "max" : NumberDecimal("199.99") },
      "count" : 2
    },
    {
      "_id" : { "min" : NumberDecimal("199.99"), "max" : NumberDecimal("385.00") },
      "count" : 2 },
    {
      "_id" : { "min" : NumberDecimal("385.00"), "max" : NumberDecimal("483.00") },
      "count" : 2
    }
  ],
  "year" : [
    { "_id" : { "min" : null, "max" : 1913 }, "count" : 3, "years" : [ 1902 ] },
    { "_id" : { "min" : 1913, "max" : 1926 }, "count" : 3, "years" : [ 1913, 1918, 1925 ] },
    { "_id" : { "min" : 1926, "max" : 1931 }, "count" : 2, "years" : [ 1926, 1931 ] }
  ]
}
相关推荐
NCIN EXPE4 小时前
redis 使用
数据库·redis·缓存
MongoDB 数据平台4 小时前
为编码代理引入 MongoDB 代理技能和插件
数据库·mongodb
极客on之路4 小时前
mysql explain type 各个字段解释
数据库·mysql
代码雕刻家5 小时前
MySQL与SQL Server的基本指令
数据库·mysql·sqlserver
lThE ANDE5 小时前
开启mysql的binlog日志
数据库·mysql
yejqvow125 小时前
CSS如何控制placeholder文字的颜色_使用--placeholder伪元素
jvm·数据库·python
oLLI PILO5 小时前
nacos2.3.0 接入pgsql或其他数据库
数据库
m0_743623925 小时前
HTML怎么创建多语言切换器_HTML语言选择下拉结构【指南】
jvm·数据库·python
pele5 小时前
Angular 表单中基于下拉选择动态启用字段必填校验的完整实现
jvm·数据库·python