发布网友 发布时间:2022-04-25 10:09
共1个回答
热心网友 时间:2022-04-08 02:42
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)