On this page
$facet (aggregation)
在本页面
Definition
$facet
- 3.4 版的新功能。
在同一 Importing 文档集的单个阶段中处理多个aggregation pipelines。每个子管道在输出文档中都有自己的字段,其结果以文档数组的形式存储。
$facet
阶段允许您创建多方面的聚合,这些聚合可表征单个聚合阶段中多个维度或方面的数据。多方面的聚合提供了多种过滤器和分类,以指导数据浏览和分析。零售商通常使用分面来缩小搜索结果的范围,方法是对产品价格,制造商,尺寸等进行过滤。
Importing 文档仅一次传递到$facet
阶段。 $facet
对同一组 Importing 文档启用各种聚合,而无需多次检索 Importing 文档。
$facet阶段具有以下形式:
{ $facet:
{
<outputField1>: [ <stage1>, <stage2>, ... ],
<outputField2>: [ <stage1>, <stage2>, ... ],
...
}
}
为每个指定的管道指定输出字段名称。
Behavior
与构面相关的聚合阶段对传入的文档进行分类和分组。在不同的$facet子管道<stage>
中指定以下与构面相关的任何阶段,以执行多方面的聚合:
aggregation stages也可以与aggregation stages一起使用,除了:
$facet
中的每个子管道都传递了完全相同的一组 Importing 文档。这些子管道彼此完全独立,并且每个子管道输出的文档数组存储在输出文档的单独字段中。一条子管线的输出不能用作同一$facet
阶段中另一条子管线的 Importing。如果需要进一步的聚合,请在$facet
之后添加其他阶段,并指定所需子管道输出的字段名称<outputField>
。
Example
考虑一个在线 Store,其库存存储在以下artwork
集合中:
{ "_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 的构面功能为 Client 提供跨多个维度(例如标签,价格和创建年份)分类的 Store 库存。此$facet阶段具有三个子管道,这些子管道使用$sortByCount,$bucket或$bucketAuto来执行此多方面的聚合。在操作开始时,artwork
的 Importing 文档仅从数据库中获取一次:
db.artwork.aggregate( [
{
$facet: {
"categorizedByTags": [
{ $unwind: "$tags" },
{ $sortByCount: "$tags" }
],
"categorizedByPrice": [
// Filter out documents without a price e.g., _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)" : [
// First bucket includes the document without a year, e.g., _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"
]
},
{
// Includes document price outside of bucket boundaries, e.g., _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 }
]
}