mongodb有能够sum的命令吗
发布网友
发布时间:2022-04-07 19:55
我来回答
共1个回答
热心网友
时间:2022-04-07 21:24
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;
});
}