$sortByCount(聚合)
在此页面上
定义
$sortByCount
版本 3.4 中的新增功能。
根据指定表达式的值对传入文档进行分组,然后计算每个不同群组中的文档数量。
每个输出文档都包含两个字段:一个包含不同组值的
_id
字段,以及一个包含属于该分组或类别的文档数量的count
字段。文档按
count
降序排序。$sortByCount
阶段具有以下原型形式:{ $sortByCount: <expression> } 字段说明expression
作为分组依据的表达式。您可以指定除文档字面值以外的任何表达式。
如需指定字段路径,请在字段名称前加上美元符号
$
,并用引号括起来。例如,要按字段employee
进行分组,请将"$employee"
指定为表达式。{ $sortByCount: "$employee" } 尽管不能指定文档字面值作为分组依据表达式,但可以指定计算结果为文档的字段或表达式。例如,如果
employee
和business
字段是文档字段,则以下$mergeObjects
表达式(其计算结果为文档)是$sortByCount
: 的有效参数:{ $sortByCount: { $mergeObjects: [ "$employee", "$business" ] } } 但是,以下带有文档字面表达式的示例无效:
{ $sortByCount: { lname: "$employee.last", fname: "$employee.first" } }
Considerations
$count
和内存限制
$sortByCount
阶段的 RAM 限制为 100 MB。默认情况下,如果阶段超出此限制,$sortByCount
将返回错误。要为阶段处理留出更多空间,请使用 allowDiskUse 选项使聚合管道阶段能够将数据写入临时文件。
行为
$sortByCount
阶段相当于以下的 $group
+ $sort
序列:
{ $group: { _id: <expression>, count: { $sum: 1 } } }, { $sort: { count: -1 } }
例子
请考虑包含以下文档的集合 exhibits
:
{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, "tags" : [ "painting", "satire", "Expressionism", "caricature" ] } { "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, "tags" : [ "woodcut", "Expressionism" ] } { "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, "tags" : [ "oil", "Surrealism", "painting" ] } { "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", "tags" : [ "woodblock", "ukiyo-e" ] } { "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, "tags" : [ "Surrealism", "painting", "oil" ] } { "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, "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, "tags" : [ "abstract", "painting" ] }
以下操作会unwinds
tags
数组并使用$sortByCount
阶段来计算每个标签相关的文档数量:
db.exhibits.aggregate( [ { $unwind: "$tags" }, { $sortByCount: "$tags" } ] )
操作将返回以下文档,按计数降序排序:
{ "_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 }