//测试数据准备
db.user.drop();
for(var i=10; i< 100; i++) {
db.user.insert({
name:"user" + i,
age : Math.floor(Math.random()*10)+ 20,
sex : Math.floor(Math.random()*3)%2 ==0 ? 'M' : 'F',
chinese : Math.floor(Math.random()*50)+50,
math : Math.floor(Math.random()*50)+50,
english : Math.floor(Math.random()*50)+50,
class : "C" + i%5
})
}
// runCommand运行方式
db.sales.runCommand({
mapreduce: "user",
map: function(){
if(this.class == "C1") {
emit(this.age, this.age);
}
},
reduce: function(key,values){
var maxValue = Max(key, values);
return maxValue;
},
{
out: {inline: 1},
query : "",
sort: "",
limit: "",
}
})
db.user.mapReduce(
// 映射函数,里面会调用emit(key,value),集合会按照你指定的key进行映射分组。
function(){
// 按照emit函数的第一个参数进行分组
// 第二个参数的值会传递给reduce
emit(this.age, this);
},
// 简化函数,会对map分组后的数据进行分组简化
// 在reduce(key,value)中的key就是emit中的key, vlaues为emit分组后的emit(value)的集合
function(key, values){
var maxValue = Math.max(key, values);
return maxValue;
},
// 可选参数
{
query: {sex: "F"},
out: "result",
sort : {},
limit : 0
}
)
{
"result" : "result", // 存放的集合名
"timeMillis" : 23,
"counts" : {
"input" : 29, // 传入文档的个数
"emit" : 29, // 此函数被调用的次数
"reduce" : 6, // 此函数被调用的次数
"output" : 8 // 最后返回文档的个数
},
"ok" : 1
}
db.result.find()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有