mongodb有能够sum的命令吗
1 回答
陪伴而非守候
TA贡献1757条经验 获得超8个赞
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;
});
}
- 1 回答
- 0 关注
- 774 浏览
添加回答
举报
0/150
提交
取消