猿问

mongodb有能够sum的命令吗?

mongodb有能够sum的命令吗


烙印99
浏览 776回答 1
1回答

陪伴而非守候

mongodb提供了聚合函数完成类似的sum函数的功能比如:$sum案例:/*** select sum(pop) total from zipsController group by state*/exports.sumGroupByStateDemoAsync = function (opts) {var results = {error_code: -1, error_msg: "error"};var pipeline =[{$group: {_id: "$state",totalpop: {$sum: "$pop"}}}];return opts.dbs.csdb.collection("zipsController").aggregateAsync(pipeline).timeout(opts.configs.timeoutnormal).then(function (value) {if (value) {results.error_code = 0;results.error_msg = "ok";results.accdetail = value;} else {//可选results.error_code = 20001;results.error_msg = "数据不存在";results.accdetail = value;}return results;});}
随时随地看视频慕课网APP

相关分类

MongoDB
我要回答