$sortByCount (aggregation)

On this page本页内容

Definition定义

$sortByCount

Groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group.根据指定表达式的值对传入文档进行分组,然后计算每个不同组中的文档数。

Each output document contains two fields: an _id field containing the distinct grouping value, and a count field containing the number of documents belonging to that grouping or category.每个输出文档包含两个字段:包含不同分组值的_id字段和包含属于该分组或类别的文档数的count字段。

The documents are sorted by count in descending order.文档按count按降序排序。

The $sortByCount stage has the following prototype form:$sortByCount阶段具有以下原型形式:

{ $sortByCount:  <expression> }
Field字段Description描述
expression

Expression to group by. 要分组的表达式You can specify any expression except for a document literal.可以指定除文档文字以外的任何表达式。

To specify a field path, prefix the field name with a dollar sign $ and enclose it in quotes. 若要指定字段路径,请在字段名称前面加上美元符号$,并将其括在引号中。For example, to group by the field employee, specify "$employee" as the expression.例如,要按字段employee进行分组,请指定"$employee"作为表达式。

{ $sortByCount:  "$employee" }

Although you cannot specify a document literal for the group by expression, you can, however, specify a field or an expression that evaluates to a document. 虽然不能为“分组依据”表达式指定文档文字,但可以指定计算结果为文档的字段或表达式。For example, if employee and business fields are document fields, then the following $mergeObjects expression, which evaluates to a document, is a valid argument to $sortByCount:例如,如果employee字段和business字段是文档字段,则计算为文档的以下$mergeObjects表达式是$sortByCount的有效参数:

{ $sortByCount: { $mergeObjects: [ "$employee", "$business" ] } }

However, the following example with the document literal expression is invalid:但是,文档文字表达式的以下示例无效:

{ $sortByCount: { lname: "$employee.last", fname: "$employee.first" } }
Tip提示
See also: 参阅:

Considerations注意事项

$count and Memory Restrictions和内存限制

The $sortByCount stage has a limit of 100 megabytes of RAM. $sortByCount阶段的RAM限制为100兆字节。By default, if the stage exceeds this limit, $sortByCount returns an error. 默认情况下,如果阶段超过此限制,$sortByCount将返回一个错误。To allow more space for stage processing, use the allowDiskUse option to enable aggregation pipeline stages to write data to temporary files.要为阶段处理留出更多空间,请使用allowDiskUse选项启用聚合管道阶段将数据写入临时文件。

Tip提示

Behavior行为

The $sortByCount stage is equivalent to the following $group + $sort sequence:$sortByCount阶段相当于以下$group+$sort序列:

{ $group: { _id: <expression>, count: { $sum: 1 } } },
{ $sort: { count: -1 } }

Example示例

Consider a collection exhibits with the following documents:考虑一个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" ] }

The following operation unwinds the tags array and uses the $sortByCount stage to count the number of documents associated with each tag:以下操作将unwinds(解开)tags数组,并使用$sortByCount阶段计算与每个标记关联的文档数:

db.exhibits.aggregate( [ { $unwind: "$tags" },  { $sortByCount: "$tags" } ] )

The operation returns the following documents, sorted in descending order by count:该操作返回以下文档,按计数降序排序:

{ "_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 }
←  $sort (aggregation)$unionWith (aggregation) →