Definition定义
$sortByCountGroups 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每个输出文档包含两个字段:一个_idfield containing the distinct grouping value, and acountfield containing the number of documents belonging to that grouping or category._id字段包含不同的分组值,一个count字段包含属于该分组或类别的文档数量。The documents are sorted by文档按countin descending order.count降序排列。The$sortByCountstage has the following prototype form:$sortByCount阶段具有以下原型形式:{ $sortByCount: <expression> }Field字段Description描述expressionExpression 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 fieldemployee, 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例如,如果employeeandbusinessfields are document fields, then the following$mergeObjectsexpression, 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" } }
Considerations注意事项
$sortByCount and Memory Restrictions和内存限制
Starting in MongoDB 6.0, pipeline stages that require more than 100 megabytes of memory to execute write temporary files to disk by default. These temporary files last for the duration of the pipeline execution and can influence storage space on your instance. 从MongoDB 6.0开始,默认情况下,需要超过100兆字节内存来执行将临时文件写入磁盘的流水线阶段。这些临时文件在管道执行期间持续存在,并可能影响实例上的存储空间。In earlier versions 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 can override the allowDiskUseByDefault parameter by either:find和aggregate命令可以通过以下任一方式覆盖allowDiskUseByDefault参数:
Using当{ allowDiskUse: true }to allow writing temporary files out to disk whenallowDiskUseByDefaultis set tofalseallowDiskUseByDefault设置为false时,使用{ allowDiskUse: true }允许将临时文件写入磁盘Using当{ allowDiskUse: false }to prohibit writing temporary files out to disk whenallowDiskUseByDefaultis set totrueallowDiskUseByDefault设置为true时,使用{ allowDiskUse: false }禁止将临时文件写入磁盘
Note
For MongoDB Atlas, it is recommended to configure storage auto-scaling to prevent long-running queries from filling up storage with temporary files.对于MongoDB Atlas,建议配置存储自动扩展,以防止长时间运行的查询用临时文件填满存储。
If your Atlas cluster uses storage auto-scaling, the temporary files may cause your cluster to scale to the next storage tier.如果Atlas集群使用存储自动扩展,临时文件可能会导致集群扩展到下一个存储层。
For additional details, see Aggregation Pipeline Limits.有关更多详细信息,请参阅聚合管道限制。
Behavior行为
The $sortByCount stage is equivalent to the following $group + $sort sequence:$sortByCount阶段相当于以下$group+$sort序列:
{ $group: { _id: <expression>, count: { $sum: 1 } } },
{ $sort: { count: -1 } }Examples示例
MongoDB Shell
Consider a collection 考虑一个包含以下文件的集合exhibits with the following documents:exhibits(展品):
db.exhibits.insertMany(
[
{ _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:$unwind展开此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 }
]C#
The C# examples on this page use the 本页上的C#示例使用Atlas示例数据集中的sample_mflix database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see Get Started in the MongoDB .NET/C# Driver documentation.sample_mflix数据库。要了解如何创建免费的MongoDB Atlas集群并加载示例数据集,请参阅MongoDB .NET/C#驱动程序文档中的入门。
The following 以下Movie class models the documents in the sample_mflix.movies collection:Movie类对sample_mflix.movies集合中的文档进行建模:
public class Movie
{
public ObjectId Id { get; set; }
public int Runtime { get; set; }
public string Title { get; set; }
public string Rated { get; set; }
public List<string> Genres { get; set; }
public string Plot { get; set; }
public ImdbData Imdb { get; set; }
public int Year { get; set; }
public int Index { get; set; }
public string[] Comments { get; set; }
[]
public DateTime LastUpdated { get; set; }
}
Note
ConventionPack for Pascal CasePascal大小写的约定包
The C# classes on this page use Pascal case for their property names, but the field names in the MongoDB collection use camel case. To account for this difference, you can use the following code to register a 此页面上的C#类使用Pascal大小写作为其属性名,但MongoDB集合中的字段名使用驼峰大小写。为了解释这种差异,您可以在应用程序启动时使用以下代码注册ConventionPack when your application starts:ConventionPack:
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);To use the MongoDB .NET/C# driver to add a 要使用MongoDB .NET/C#驱动程序将$sortByCount stage to an aggregation pipeline, call the SortByCount() method on a PipelineDefinition object.$sortByCount阶段添加到聚合管道中,请在PipelineDefinition对象上调用SortByCount()方法。
The following example creates a pipeline stage that groups the incoming 以下示例创建了一个管道阶段,该阶段根据Movie documents by the value of their Rated field, and then returns the number of documents in each group:Rated字段的值对传入的Movie文档进行分组,然后返回每个组中的文档数量:
var pipeline = new EmptyPipelineDefinition<Movie>()
.SortByCount(m => m.Rated);Node.js
The Node.js examples on this page use the 本页上的Node.js示例使用Atlas示例数据集中的sample_mflix database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see Get Started in the MongoDB Node.js driver documentation.sample_mflix数据库。要了解如何创建免费的MongoDB Atlas集群并加载示例数据集,请参阅MongoDB Node.js驱动程序文档中的入门。
To use the MongoDB Node.js driver to add a 要使用MongoDB Node.js驱动程序将$sortByCount stage to an aggregation pipeline, use the $sortByCount operator in a pipeline object.$sortByCount阶段添加到聚合管道中,请在管道对象中使用$sortByCount运算符。
The following example creates a pipeline stage that groups the incoming 以下示例创建了一个管道阶段,该阶段根据其movie documents by the value of their rated field, and then returns the number of documents in each group. The example then runs the aggregation pipeline:rated字段的值对传入的movie文档进行分组,然后返回每个组中的文档数量。然后,该示例运行聚合管道:
const pipeline = [{ $sortByCount: "$rated" }];
const cursor = collection.aggregate(pipeline);
return cursor;