外贸网站建设要求,微信小程序外包价目表,新人跑业务怎么找客户,直播网站建设需要什么软件有哪些$bucket将输入文档按照指定的表达式和边界进行分组#xff0c;每个分组为一个文档#xff0c;称为“桶”#xff0c;每个桶都有一个唯一的_id#xff0c;其值为文件桶的下线。每个桶中至少要包含一个输入文档#xff0c;也就是没有空桶。
使用
语法
{$bucket: {groupBy…$bucket将输入文档按照指定的表达式和边界进行分组每个分组为一个文档称为“桶”每个桶都有一个唯一的_id其值为文件桶的下线。每个桶中至少要包含一个输入文档也就是没有空桶。
使用
语法
{$bucket: {groupBy: 表达式,boundaries: [ 下边界1, 下边界2, ... ],default: literal,output: {output1: { $accumulator 表达式 },...outputN: { $accumulator 表达式 }}}
}groupBy
对文档进行分组的表达式。若指定字段路径需要在字段名前加上美元符号$并用引号引起来如$field_name。
除非指定了default否则所有输入文档的groupBy的值都必须在boundaries指定边界的范围内。
boundaries
分组边界数组数组中相邻的两个值分别作为桶的上下边界输入文档根据groupBy表达式的值确定被分配到哪个桶。数组至少要有两个元素并按照升序从左到右排列除数值混合类型外如[10, NumberLong(20), NumberInt(30)]数组元素类型必须一致。
举例
一个数组 [ 0, 5, 10 ] 创建了两个桶
[05下界为 0上界为 5。
[510下界为 5上界为 10。
default
可选指定缺省桶的_id不符合boundaries范围的文档都会放在缺省桶内。如果不指定default所有输入文档的groupBy表达式的值必须落在boundaries区间否则会抛出异常。
缺省值必须小于boundaries数组中最小的值或大于boundaries数组中的最大值。default值的类型可以不同于boundaries数组元素的类型。
out
可选指定输出文档内容中除_id字段外要包含的其他字段指定的字段必须使用汇总累加器表达式。
outputfield1: { accumulator: expression1 },
...
outputfieldN: { accumulator: expressionN }如果未指定output文档默认返回桶内文档数量count字段如果指定了output文档的字段则只返回_id和指定的字段count字段默认不会输出。
例子
按年分桶并对桶的结果进行筛选
创建artists集合并插入下面的记录
db.artists.insertMany([{ _id : 1, last_name : Bernard, first_name : Emil, year_born : 1868, year_died : 1941, nationality : France },{ _id : 2, last_name : Rippl-Ronai, first_name : Joszef, year_born : 1861, year_died : 1927, nationality : Hungary },{ _id : 3, last_name : Ostroumova, first_name : Anna, year_born : 1871, year_died : 1955, nationality : Russia },{ _id : 4, last_name : Van Gogh, first_name : Vincent, year_born : 1853, year_died : 1890, nationality : Holland },{ _id : 5, last_name : Maurer, first_name : Alfred, year_born : 1868, year_died : 1932, nationality : USA },{ _id : 6, last_name : Munch, first_name : Edvard, year_born : 1863, year_died : 1944, nationality : Norway },{ _id : 7, last_name : Redon, first_name : Odilon, year_born : 1840, year_died : 1916, nationality : France },{ _id : 8, last_name : Diriks, first_name : Edvard, year_born : 1855, year_died : 1930, nationality : Norway }
])下面的操作对文档按照year_born字段进行分组放入桶中并根据桶内文档数量进行筛选
db.artists.aggregate( [// 阶段1{$bucket: {groupBy: $year_born, // 分组字段boundaries: [ 1840, 1850, 1860, 1870, 1880 ], // 桶边界default: Other, // 边界外的桶的IDoutput: { // 指定桶的输出文档count: { $sum: 1 },artists :{$push: {name: { $concat: [ $first_name, , $last_name] },year_born: $year_born}}}}},// 阶段2{$match: { count: {$gt: 3} } //过滤出文档数量大于3的桶}
] )阶段1
$bucket阶段对文档根据year_born分组把文档放入桶桶的边界为
[1840, 1850)下限1840含上限1850不含。[1850, 1860)下限1840含上限1850不含。[1860, 1870)下限1840含上限1850不含。[1870, 1880)下限1840含上限1850不含。如果输入文档中year_born字段不存在或者值在边界外文档将被放到_id值为other的缺省桶中。
阶段1的output指定了输出文档的字段
字段描述_id包含了桶的边界下限count桶内文档数量artists文档数组包含了桶内所有文章每个文档的artists字段都包含了拼接后的first_name和last_name以及year_born’字段
通过该阶段后下面的文档进入下个阶段
{ _id : 1840, count : 1, artists : [ { name : Odilon Redon, year_born : 1840 } ] }
{ _id : 1850, count : 2, artists : [ { name : Vincent Van Gogh, year_born : 1853 },{ name : Edvard Diriks, year_born : 1855 } ] }
{ _id : 1860, count : 4, artists : [ { name : Emil Bernard, year_born : 1868 },{ name : Joszef Rippl-Ronai, year_born : 1861 },{ name : Alfred Maurer, year_born : 1868 },{ name : Edvard Munch, year_born : 1863 } ] }
{ _id : 1870, count : 1, artists : [ { name : Anna Ostroumova, year_born : 1871 } ] }阶段2
$match阶段使用count3的条件对$bucket阶段out的文档进行筛选筛选后的结果如下
{ _id : 1860, count : 4, artists :[{ name : Emil Bernard, year_born : 1868 },{ name : Joszef Rippl-Ronai, year_born : 1861 },{ name : Alfred Maurer, year_born : 1868 },{ name : Edvard Munch, year_born : 1863 }]
}使用$bucket和$facet按多个字段分类
使用$facet可以在一个阶段执行多个$bucket聚合。使用mongosh创建artwork集合并添加下面的文档
db.artwork.insertMany([{ _id : 1, title : The Pillars of Society, artist : Grosz, year : 1926,price : NumberDecimal(199.99) },{ _id : 2, title : Melancholy III, artist : Munch, year : 1902,price : NumberDecimal(280.00) },{ _id : 3, title : Dancer, artist : Miro, year : 1925,price : NumberDecimal(76.04) },{ _id : 4, title : The Great Wave off Kanagawa, artist : Hokusai,price : NumberDecimal(167.30) },{ _id : 5, title : The Persistence of Memory, artist : Dali, year : 1931,price : NumberDecimal(483.00) },{ _id : 6, title : Composition VII, artist : Kandinsky, year : 1913,price : NumberDecimal(385.00) },{ _id : 7, title : The Scream, artist : Munch, year : 1893/* No price*/ },{ _id : 8, title : Blue Flower, artist : OKeefe, year : 1918,price : NumberDecimal(118.42) }
])下面的操作在一个$facet阶段中使用两个$bucket一个使用price字段另一个使用year字段分组
db.artwork.aggregate( [{$facet: { // 顶层 $facet 阶段price: [ // 输出字段1{$bucket: {groupBy: $price, // 分组字段boundaries: [ 0, 200, 400 ], // 桶边界数组default: Other, // 缺省桶Idoutput: { // 桶输出内容count: { $sum: 1 },artwork : { $push: { title: $title, price: $price } },averagePrice: { $avg: $price }}}}],year: [ // 输出字段2{$bucket: {groupBy: $year, // 分组字段boundaries: [ 1890, 1910, 1920, 1940 ], // 桶边界数组default: Unknown, // 缺省桶Idoutput: { // 桶输出内容count: { $sum: 1 },artwork: { $push: { title: $title, year: $year } }}}}]}}
] )方面1
第一个方面按price对输入文档进行分组桶的边界有
[0,200)含下限0不含上限200。[200, 400)含下限200不含上限400。“Other”缺省桶包含了所有不在以上桶内的文档。
$bucket阶段的输出out文档包含下面的字段
字段描述_id桶边界下限值count桶内文档数量artwork包含所有艺术品信息的文档数组averagePrice使用$avg运算符显示水桶中所有艺术品的平均价格。
方面2
第二个方面按year对输入文档进行分组桶的边界有
[1890, 1910)含下限1890不含上限1910。[1910, 1920)含下限1890不含上限1910。[1920, 1940)含下限1890不含上限1910。“Unknown”缺省桶包含了所有不在以上桶内的文档。
$bucket阶段的输出out文档包含下面的字段
字段描述count桶内文档数量artwork桶内每件艺术品信息的文件数组。
输出
操作返回下面的结果
{price : [ // Output of first facet{_id : 0,count : 4,artwork : [{ title : The Pillars of Society, price : NumberDecimal(199.99) },{ title : Dancer, price : NumberDecimal(76.04) },{ title : The Great Wave off Kanagawa, price : NumberDecimal(167.30) },{ title : Blue Flower, price : NumberDecimal(118.42) }],averagePrice : NumberDecimal(140.4375)},{_id : 200,count : 2,artwork : [{ title : Melancholy III, price : NumberDecimal(280.00) },{ title : Composition VII, price : NumberDecimal(385.00) }],averagePrice : NumberDecimal(332.50)},{// Includes documents without prices and prices greater than 400_id : Other,count : 2,artwork : [{ title : The Persistence of Memory, price : NumberDecimal(483.00) },{ title : The Scream }],averagePrice : NumberDecimal(483.00)}],year : [ // Output of second facet{_id : 1890,count : 2,artwork : [{ title : Melancholy III, year : 1902 },{ title : The Scream, year : 1893 }]},{_id : 1910,count : 2,artwork : [{ title : Composition VII, year : 1913 },{ title : Blue Flower, year : 1918 }]},{_id : 1920,count : 3,artwork : [{ title : The Pillars of Society, year : 1926 },{ title : Dancer, year : 1925 },{ title : The Persistence of Memory, year : 1931 }]},{// Includes documents without a year_id : Unknown,count : 1,artwork : [{ title : The Great Wave off Kanagawa }]}]
}注意
跟很多阶段类似$bucket阶段也有100M内存的限制缺省情况下如果超出100M将会抛出异常。可使用allowDiskUse选项让聚合管道阶段将数据写入临时文件。