对输入的文档执行多个聚合管道,在输出结果中,每个子管道一个字段,字段值是一个文档数组。
$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" : "O'Keefe", "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 }]
}