网站正在建设中 动态,徐州网站建设方案咨询,wordpress文章图片全屏浏览,怎么做网站 ppt对输入的文档执行多个聚合管道#xff0c;在输出结果中#xff0c;每个子管道一个字段#xff0c;字段值是一个文档数组。
$facet可以在一个阶段创建多面聚合#xff0c;横跨多个维度或方面来描述数据特征。多面聚合可提供多个过滤器和分类指导数据浏览和分析。
$facet 阶…对输入的文档执行多个聚合管道在输出结果中每个子管道一个字段字段值是一个文档数组。
$facet可以在一个阶段创建多面聚合横跨多个维度或方面来描述数据特征。多面聚合可提供多个过滤器和分类指导数据浏览和分析。
$facet 阶段在单个聚合阶段内创建多面聚合通过多个维度或面来描述数据特征。多面聚合提供了多个过滤器和分类为数据浏览和分析导航零售商通常通过创建产品价格、制造商、尺寸等方面的过滤器以及使用分面来缩小搜索范围。
输入文件只传递给$facet阶段一次。$facet可对同一组输入文档进行各种聚合而无需多次检索输入文档。
语法
$facet阶段使用方法如下
{ $facet:{outputField1: [ stage1, stage2, ... ],outputField2: [ stage1, stage2, ... ],...}
}使用 注意$facet的每个阶段执行时返回文档大小不能超过100M而且由于$facet不能缓存到磁盘所以即使指定了allowDiskUse标志也没用。 指定下面任何一个方面相关的阶段在不同的$facet子管道的stage去执行一个已多方面的聚合. $bucket$bucketAuto$sortByCount 下面阶段中不能使用$facet $collStats$facet$geoNear$indexStats$out$merge$planCacheStats $facet的所有子管道都传递完全相同的输入文档这些子管道彼此独立每个子管道输出的文档数组都存储在各自的字段中。另外一个子管道的输出不能用作同一个$facet阶段内其它子管道的输入。如果需要进一步聚合可以在$facet后面添加其他阶段。 $facet 中的每个子管道都会传递完全相同的输入文档集。这些子流水线彼此完全独立每个子流水线输出的文档数组都存储在输出文档的不同字段中。一个子管道的输出不能用作同一 $facet 阶段内不同子管道的输入。如果需要进一步聚合可以在\$facet的后面添加其他阶段并指定所需次级管道输出的字段名称 outputField。 管道的顺序决定了$facet使用索引 如果$facet是管道的第一个阶段将会执行COLLSCAN不会使用索引。如果$facet在其他阶段之后并且先前的阶段已经使用了索引则$facet在执行的时候不会触发COLLSCAN。比如如果$match或$sort阶段在$facet之前则$facet使用索引并且不会触发COLLSCAN。
举例
以下在线艺术品商店库存中存在以下的艺术藏品
{ _id : 1, title : The Pillars of Society, artist : Grosz, year : 1926,price : NumberDecimal(199.99),tags : [ painting, satire, Expressionism, caricature ] }
{ _id : 2, title : Melancholy III, artist : Munch, year : 1902,price : NumberDecimal(280.00),tags : [ woodcut, Expressionism ] }
{ _id : 3, title : Dancer, artist : Miro, year : 1925,price : NumberDecimal(76.04),tags : [ oil, Surrealism, painting ] }
{ _id : 4, title : The Great Wave off Kanagawa, artist : Hokusai,price : NumberDecimal(167.30),tags : [ woodblock, ukiyo-e ] }
{ _id : 5, title : The Persistence of Memory, artist : Dali, year : 1931,price : NumberDecimal(483.00),tags : [ Surrealism, painting, oil ] }
{ _id : 6, title : Composition VII, artist : Kandinsky, year : 1913,price : NumberDecimal(385.00),tags : [ oil, painting, abstract ] }
{ _id : 7, title : The Scream, artist : Munch, year : 1893,tags : [ Expressionism, painting, oil ] }
{ _id : 8, title : Blue Flower, artist : OKeefe, year : 1918,price : NumberDecimal(118.42),tags : [ abstract, painting ] }下面的操作使用MongoDB的分面功能为客户提供按标签、价格和创建年份等多个维度分类的商店库存。$facet阶段有三个子管道分别使用$sortByCount、$bucket或$bucketAuto来执行多面聚合。输入文档只在操作开始时从数据库中获取一次
db.artwork.aggregate( [{$facet: {categorizedByTags: [{ $unwind: $tags },{ $sortByCount: $tags }],categorizedByPrice: [//过滤掉缺失价格的文档 如_id: 7的文档{ $match: { price: { $exists: 1 } } },{$bucket: {groupBy: $price,boundaries: [ 0, 150, 200, 300, 400 ],default: Other,output: {count: { $sum: 1 },titles: { $push: $title }}}}],categorizedByYears(Auto): [{$bucketAuto: {groupBy: $year,buckets: 4}}]}}
])操作返回结果
{categorizedByYears(Auto) : [// 第一个桶包括不带年份的文件,如_id: 4{ _id : { min : null, max : 1902 }, count : 2 },{ _id : { min : 1902, max : 1918 }, count : 2 },{ _id : { min : 1918, max : 1926 }, count : 2 },{ _id : { min : 1926, max : 1931 }, count : 2 }],categorizedByPrice : [{_id : 0,count : 2,titles : [Dancer,Blue Flower]},{_id : 150,count : 2,titles : [The Pillars of Society,The Great Wave off Kanagawa]},{_id : 200,count : 1,titles : [Melancholy III]},{_id : 300,count : 1,titles : [Composition VII]},{//包含桶边界外的文档的价格 如_id: 5_id : Other,count : 1,titles : [The Persistence of Memory]}],categorizedByTags : [{ _id : painting, count : 6 },{ _id : oil, count : 4 },{ _id : Expressionism, count : 3 },{ _id : Surrealism, count : 2 },{ _id : abstract, count : 2 },{ _id : woodblock, count : 1 },{ _id : woodcut, count : 1 },{ _id : ukiyo-e, count : 1 },{ _id : satire, count : 1 },{ _id : caricature, count : 1 }]
}