关于mongodb的aggregate问题,求解答
发布网友
发布时间:2022-04-08 03:10
我来回答
共1个回答
热心网友
时间:2022-04-08 04:40
aggregate是monogodb的聚合函数。完成类似于sql中的sum,count,group by等相关操作
1:count(*)
/**
* select count(*) count from zipsController
*/
var pipeline = [
{
$group: {
_id: null,
count: {$sum: 1}
}
}
];
2:sum(*)
/**
* select sum(pop) total from zipsController
*/
var pipeline = [
{
$group: {
_id: null,
total: {$sum: "$pop"}
}
}
];
3:group by
/**
* select sum(pop) total from zipsController group by state
*/
var pipeline =[
{
$group: {
_id: "$state",
totalpop: {$sum: "$pop"}
}
}
];