$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 acount
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
andbusiness
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" } }
TipSee also:另请参阅:
Considerations注意事项
$sortByCount
and Memory Restrictions和内存限制
$sortByCount
is subject to the 100 megabyte memory usage limit, but is able to write temporary files to disk if additional space is required.受100兆字节内存使用限制,但如果需要额外空间,可以将临时文件写入磁盘。
Starting in MongoDB 6.0, pipeline stages that require more than 100 megabytes of memory to execute write temporary files to disk by default. 从MongoDB 6.0开始,需要超过100MB内存才能执行的管道阶段默认情况下会将临时文件写入磁盘。In earlier verisons of MongoDB, you must pass 在MongoDB的早期版本中,必须将{ allowDiskUse: true }
to individual find
and aggregate
commands to enable this behavior.{ allowDiskUse: true }
传递给单个find
和aggregate
命令才能启用此行为。
Individual 单独的find
and aggregate
commands may override the allowDiskUseByDefault
parameter by either:find
和aggregate
命令可以通过以下方式覆盖allowDiskUseByDefault
参数:
Using当{ allowDiskUse: true }
to allow writing temporary files out to disk whenallowDiskUseByDefault
is set tofalse
allowDiskUseByDefault
设置为false
时,使用{ allowDiskUse: true }
允许将临时文件写入磁盘Using当{ allowDiskUse: false }
to prohibit writing temporary files out to disk whenallowDiskUseByDefault
is set totrue
allowDiskUseByDefault
设置为true
时,使用{ allowDiskUse: false }
禁止将临时文件写入磁盘
See also: 另请参阅:
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: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 }