准备测试数据
Db.user.drop();
对于(var i = 10;i < 100;i + +){
Db.user.insert({
名称:用户+ i,
年龄:Math.floor(Math.random()* 10)+ 20,
性别:Math.floor(Math.random)*(3)% 2 = = 0是:F,
中文:Math.floor(Math.random()* 50)+ 50,
数学:Math.floor(Math.random()* 50)+ 50,
英语:math.floor(Math.random()* 50)+ 50,
类:C+ i % 5
})
}

组函数
根据类进行分组,在每个类中显示用户名和性别。
db.user.group({
键:{类):真},
初始值:{人:{ },
减少:功能(CUR,沪指){
Prev.person.push({姓名:性别:cur.name,cur.sex,年龄:恶狗。年龄});
}
});

年龄> 25个用户,按类分组,显示每个类中的用户名和性别,以及每个组的统计数。
db.user.group({
键:{类):真},
初始值:{人:{ },
减少:函数(doc,out){
Out.person.push({姓名:性别:doc.name,doc.sex,年龄:医生年龄});
},
终结:函数(out){
out.count = out.person.length;
},
条件:{年龄):{ $ gt 25 }
})

每个类的分组计算,中文最大值和最小值
db.user.group({
键:{类):真},
初始:{chinese_min :0,chinese_max :0 },
减少:函数(doc,out){
out.chinese_min = doc.chinese;
out.chinese_min = doc.chinese;

out.chinese_min = Math.min(out.chinese_min,博士,中国);
out.chinese_max = math.max(out.chinese_max,博士,中国)
},
})

使用组,计算每个值的总成绩和平均分数。
db.user.group({
关键词:{_id ):true},
初始化:{名称:
减少:函数(doc,out){
out.name = doc.name;
out.total = doc.chinese + doc.math + doc.english;
out.avg = math.floor(out.total / 3);
}
})



组参数选项:

1.key:这是团队的关键

2.initial:每一组股票的初始化函数,特别注意:初始函数每一集。

3.reduce:这个函数的第一个参数是当前文档对象,第二个参数是最后一个函数运行累计对象。多少文件有多少倍的减少将给美元。

4.condition:这是过滤条件。

5.finalize:这是一个函数,将触发此方法时,每一套文件执行。