首页 > 其他分享 >$count (aggregation)

$count (aggregation)

时间:2023-08-12 09:13:12浏览次数:40  
标签:count aggregation score stage id History

https://www.mongodb.com/docs/manual/reference/operator/aggregation/count/

 

$count (aggregation)

   
$count
 

Passes a document to the next stage that contains a count of the number of documents input to the stage.

NOTE

Disambiguation

This page describes the $count aggregation pipeline stage. For the $count aggregation accumulator, see $count (aggregation accumulator).

$count has the following prototype form:

{ $count: <string> }
 

<string> is the name of the output field which has the count as its value. <string> must be a non-empty string, must not start with $ and must not contain the . character.

TIP

See also:

 

The $count stage is equivalent to the following $group + $project sequence:

db.collection.aggregate( [
{ $group: { _id: null, myCount: { $sum: 1 } } },
{ $project: { _id: 0 } }
] )
 

where myCount would be the output field that contains the count. You can specify another name for the output field.

TIP

See also:

db.collection.countDocuments() which wraps the $group aggregation stage with a $sum expression.

 

A collection named scores has the following documents:

{ "_id" : 1, "subject" : "History", "score" : 88 }
{ "_id" : 2, "subject" : "History", "score" : 92 }
{ "_id" : 3, "subject" : "History", "score" : 97 }
{ "_id" : 4, "subject" : "History", "score" : 71 }
{ "_id" : 5, "subject" : "History", "score" : 79 }
{ "_id" : 6, "subject" : "History", "score" : 83 }
 

The following aggregation operation has two stages:

  1. The $match stage excludes documents that have a score value of less than or equal to 80 to pass along the documents with score greater than 80 to the next stage.

  2. The $count stage returns a count of the remaining documents in the aggregation pipeline and assigns the value to a field called passing_scores.

db.scores.aggregate(
[
{
$match: {
score: {
$gt: 80
}
}
},
{
$count: "passing_scores"
}
]
)
 

The operation returns the following results:

{ "passing_scores" : 4 }
 

标签:count,aggregation,score,stage,id,History
From: https://www.cnblogs.com/kungfupanda/p/17624338.html

相关文章

  • mysql 高性能count表
    --mysql高性能count表SELECTSQL_CALC_FOUND_ROWS1FROMtable_namelimit1;SELECTfound_rows()ASrowcount;selectcount(1)ascntfromtable_name;--35G1102888行的表--2.13sec和29.84sec--11G14216365--28.60sec和48.43sec--结论:......
  • C# 使用Environment.TickCount 实现的时间限制器,限制一定时间内只能执行一次操作,避免
    之前都是通过DateTime.UtcNow来获取时间间隔,后来发现 Environment.TickCount相比DateTime.UtvNow快了20多倍,就用Environment.TickCount实现了个限制器测试代码staticTimeLimitertimeLimiter=newTimeLimiter();staticvoidTest(){while......
  • 【转载】The City Mouse and the Country Mouse
    ArticleFormatSourceChatGPTMainContentSourceTitle:TheCityMouseandtheCountryMouseSource:https://www.zhihu.com/question/263840407/answer/1108124796Author:苏焉儿MainContentOncethereweretwomice.Theywerefriends.Onemouselivedintheco......
  • 题解 CF1857G【Counting Graphs】
    一个非常显然的事情是:总方案数即为每条边方案数之积。树边已经确定,考察每条非树边\((u,v)\)可以怎么取。给定的树\(T\)是唯一最小生成树,这意味着非树边\((u,v)\)要么不存在,要么权值大于\(T\)上\((u,v)\)之间任意一条边的权值。设\(T\)上\((u,v)\)间的最大边权为\(......
  • G. Counting Graphs
    G.CountingGraphsGivenatreeconsistingof$n$vertices.Atreeisaconnectedundirectedgraphwithoutcycles.Eachedgeofthetreehasitsweight,$w_i$.Yourtaskistocountthenumberofdifferentgraphsthatsatisfyallfourconditions:Thegra......
  • wordpress 插件 woocommerce对billing address和account details界面进行修改
    wordpress插件woocommerce高级使用对界面结构进行修改:参考HowToEditFiles只修改部分fieldbillingaddressadd_filter('woocommerce_billing_fields',function($billing_fields){unset($billing_fields['billing_email']);return$billing_fields;},10......
  • 数字滚动插件vue-countup-v2
    参考博客https://blog.csdn.net/weixin_44948981/article/details/123544242options参数说明duration:2,//动画持续时间(秒)useEasing:true,//使用缓动效果useGrouping:true,//使用分组分隔符(如1,000)separator:',',//分组分隔符decimal:'.',//小数点符号pref......
  • DataFrame 计数value_counts 后转成df
    importpandasaspd#创建示例DataFramedata={'Category':['A','B','A','C','A','B','C','A','B']}df=pd.DataFrame(data)#使用value_counts()方法对&......
  • OpenERP分析会计(Analytic Account)应用案例
    AnalyticAccount,分析会计,也叫管理会计、成本会计。分析会计有何应用呢?试看两个例子:1)佣金计算,对于每一张销售订单(SO,SalesOrder),业务员提成销售额的1%,关系人返点(回扣)5%。如何配置OpenERP系统以实现本业务需求呢?2)制造费用分配,我们知道生产成本核算中,先要按标准费率计算加工费用,......
  • COUNTBLANK函数与其它COUNT家族函数的对比!
    1职场实例今天有位小伙伴想要统计一下某列数据的“空白单元格”的个数。如下图所示:A列有一列数据。A2单元格为数值,A3单元格为文本值,A4单元格为字母,A5单元格为真空值,A6单元格为由公式IF(A5="","")产生的空值,A7单元格为逻辑值,A8单元格为数值,A9单元格为文本型数值。我们想要统计一下空......