node.js - 使用 mongodb 对所有文档求和
问题描述
我有一个包含三个文档的集合:
{ "_id" : ObjectId("5bfe572882ace71e43703d15"), "event" : "Cdr", "privilege" : "cdr,all", "accountcode" : "", "source" : "22000", "destination" : "98723546", "destinationcontext" : "from-internal", "callerid" : "\"22000\" <22000>", "channel" : "SIP/22000-00000005", "destinationchannel" : "SIP/9144502101-00000006", "lastapplication" : "Dial", "lastdata" : "SIP/9144502101/98723546,300,Tb(func-apply-sipheaders^s^1)", "starttime" : "2018-11-28 15:51:47", "answertime" : "", "endtime" : "2018-11-28 15:51:52", "duration" : "5", "billableseconds" : "0", "disposition" : "NO ANSWER", "amaflags" : "DOCUMENTATION", "uniqueid" : "1543395107.5", "userfield" : "" }
{ "_id" : ObjectId("5bfe5829b3a9321f241f10f2"), "event" : "Cdr", "privilege" : "cdr,all", "accountcode" : "", "source" : "98723546", "destination" : "s", "destinationcontext" : "ivr-1", "callerid" : "\"98723546\" <98723546>", "channel" : "SIP/9144502101-00000007", "destinationchannel" : "", "lastapplication" : "BackGround", "lastdata" : "custom/int1", "starttime" : "2018-11-28 15:56:03", "answertime" : "2018-11-28 15:56:03", "endtime" : "2018-11-28 15:56:09", "duration" : "6", "billableseconds" : "6", "disposition" : "ANSWERED", "amaflags" : "DOCUMENTATION", "uniqueid" : "1543395363.7", "userfield" : "" }
{ "_id" : ObjectId("5bfe5833b3a9321f241f10f4"), "event" : "Cdr", "privilege" : "cdr,all", "accountcode" : "", "source" : "98723546", "destination" : "22000", "destinationcontext" : "from-did-direct", "callerid" : "\"98723546\" <98723546>", "channel" : "SIP/9144502101-00000008", "destinationchannel" : "SIP/22000-00000009", "lastapplication" : "Dial", "lastdata" : "SIP/22000,,HhtrIb(func-apply-sipheaders^s^1)", "starttime" : "2018-11-28 15:56:12", "answertime" : "2018-11-28 15:56:12", "endtime" : "2018-11-28 15:56:19", "duration" : "7", "billableseconds" : "7", "disposition" : "NO ANSWER", "amaflags" : "DOCUMENTATION", "uniqueid" : "1543395372.8", "userfield" : "" }
我想总结 $duration 并尝试命令:
db.cdrs.aggregate([{$group: { _id: "$event", total: { $sum: "$duration"}}}])
结果返回:
{ "_id" : "Cdr", "total" : 0 }
如何使用 $sum 返回结果 三个文档的总和持续时间(5 + 6 + 7 = 18)?
解决方案
推荐阅读
- python - 在python中运行多处理时如何修复错误“无法序列化'_io.BufferedReader'对象”
- python - 使用 nltk 雪球词干分析器将列中的值作为参数传递
- python - lambda with condition x: 1/x if x!= 0 not work
- r - R动作按钮和eventReactive不起作用
- git - 我正在使用 git lab api 但我不知道如何请求 BOOLEAN 类型值
- c# - 在 C# 中获取节点 XML
- ios - iOS是否允许将用户重定向到网站进行付款?
- conditional-statements - 如果购物篮中有 X 或 Y 类别的产品,则在结帐时显示消息
- windows - 允许多个进程共享一个 DLL
- android - FIDO2 MAKE_CREDENTIAL 响应问题