On this page本页内容
$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> }
expression |
{ $sortByCount: "$employee" }
{ $sortByCount: { $mergeObjects: [ "$employee", "$business" ] } }
{ $sortByCount: { lname: "$employee.last", fname: "$employee.first" } } |
$count
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
选项启用聚合管道阶段将数据写入临时文件。
The $sortByCount
stage is equivalent to the following $group
+ $sort
sequence:$sortByCount
阶段相当于以下$group
+$sort
序列:
{ $group: { _id: <expression>, count: { $sum: 1 } } }, { $sort: { count: -1 } }
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 }