$sortByCount (aggregation)

On this page本页内容

Definition定义

$sortByCount

New in version 3.4.版本3.4中的新功能。

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. 尽管不能为group by表达式指定文档文字,但可以指定计算结果为文档的字段或表达式。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 $sortByCounts:例如,如果employeebusiness字段是文档字段,则以下$mergeObjects表达式(其计算结果为文档)是$sortByCounts的有效参数:

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

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

{ $sortByCount: { lname: "$employee.last", fname: "$employee.first" } }

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:以下操作将unwindstags数组,并使用$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 }