On this page本页内容
$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
阶段的原型形式如下:
expression |
|
See also参阅
The $sortByCount
stage is equivalent to the following $group
+ $sort
sequence:$sortByCount
阶段相当于以下$group
+ $sort
序列:
Consider a collection 考虑一个集合exhibits
with the following documents:exhibits
带有的下列文档:
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
阶段统计与每个标记关联的文档数:
The operation returns the following documents, sorted in descending order by count:该操作返回以下文档,按计数降序排列: