文档菜单
文档首页
/
MongoDB 手册
/ / /

$facet (聚合)

本页内容

  • 定义
  • 兼容性
  • 语法
  • 注意事项
  • 行为
  • 示例
$facet

处理多个在单个阶段中对同一组输入文档执行多个聚合管道。每个子管道在输出文档中都有其自己的字段,其中其结果以文档数组的形式存储。

$facet阶段允许您创建多面聚合,以在单个聚合阶段中描述跨多个维度或方面的数据。多面聚合提供多个过滤和分类,以引导数据浏览和分析。零售商通常使用多面来通过创建产品价格、制造商、尺寸等过滤器来缩小搜索结果。

输入文档仅传递到$facet阶段一次。$facet使您能够对同一组输入文档执行各种聚合,而无需多次检索输入文档。

您可以使用$facet在以下环境中部署

  • MongoDB Atlas:云中MongoDB部署的全托管服务

《$facet》阶段的形式如下

{ $facet:
{
<outputField1>: [ <stage1>, <stage2>, ... ],
<outputField2>: [ <stage1>, <stage2>, ... ],
...
}
}

指定每个指定管道的输出字段名称。

随着每个《$facet》阶段的执行,结果文档大小限制为100兆字节。请注意,allowDiskUse标志不会影响100兆字节的大小限制,因为《$facet》不能溢出到磁盘。

最终输出文档受到16兆字节BSON文档大小限制。如果超过16兆字节,聚合操作将产生错误。

提示

另请参阅

与分面相关的聚合阶段对传入的文档进行分类和分组。在各个$facet子流水线的<stage>中指定以下任何一个与分面相关的阶段,以执行多分面聚合

以下$facet以外的其他聚合阶段也可以与$facet一起使用,但有以下例外

$facet中的每个子流水线都接收完全相同的输入文档集。这些子流水线之间完全独立,并且每个子流水线输出的文档数组存储在输出文档的单独字段中。一个子流水线的输出不能用作同一$facet阶段中另一个子流水线的输入。如果需要进一步聚合,请在$facet之后添加额外的阶段,并指定所需子流水线输出的字段名,即<outputField>

流水线顺序决定了$facet阶段如何使用索引。

  • 如果$facet阶段是流水线中的第一个阶段,该阶段将执行COLLSCAN。如果$facet阶段是流水线中的第一个阶段,则该阶段不会使用索引。

  • 如果$facet阶段在流水线中的位置较晚且较早的阶段已经使用索引,则$facet在执行期间不会触发COLLSCAN

例如,在$facet阶段之前的$match$sort阶段可以使用索引,并且$facet不会触发COLLSCAN

有关优化建议,请参阅:聚合流水线优化。

考虑一个库存存储在以下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的细分功能为用户提供按多个维度(如标签、价格和创建年份)分类的商店库存。此$facet阶段包含三个子管道,分别使用$sortByCount$bucket$bucketAuto来执行此多细分聚合。从数据库中获取artwork的输入文档只在操作开始时进行一次

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 }
]
}

返回

$documents