mongodb中如何实现类似于sql中的groupby?
1 回答
动漫人物
TA贡献1815条经验 获得超10个赞
mongodb中实现sql中德group by可以采用聚合函数aggregate
参考代码:
/**
* select avg(pop) avgCitypop from zipsController group by state
*/
exports.avgGroupByStateCityDemo2Async = function (opts) {
var results = {error_code: -1, error_msg: "error"};
var pipeline =[
{
$group: {
_id: "$state",
avgCitypop: {$avg: "$pop"}
}
}
];
return opts.dbs.csdb.collection("zipsController").aggregateAsync(pipeline)
- 1 回答
- 0 关注
- 1369 浏览
添加回答
举报
0/150
提交
取消