mongodb的mapreduce实现分组统计数量

分组:test

db.runCommand(
 { mapreduce : "test",
    map : function() {
					  var nameKey = this.name;
					  kws.forEach(function(kw){
					  var key = kw.key;
						if(nameKey.indexOf(key) == 0){
							nameKey = nameKey.substring(key.length, nameKey.length); 
						}
					  });
                      var key = nameKey;
                      var value = {
                                    count: 1
                                   };

                      emit( key, value );
                  },
    reduce : function(key, values) {

					var reducedObject = {
										  name: key,
										  count:0
										};

					values.forEach( function(value) {
										  reducedObject.name = reducedObject.name;
										  reducedObject.count += value.count;
									}
								  );
					return reducedObject;
				 },
	scope:{kws:[{key:"xm: "},{key:"xm:"}]},
	out: { reduce: "test_group" },
	verbose:true
 }
);

travel_forum
分组:title

db.runCommand(
 { mapreduce : "travel_forum",
    map : function() {
                      var titleKey = this.title;
					  kws.forEach(function(kw){
						var key = kw.key;
						if(titleKey.indexOf(key) == 0){
							titleKey = titleKey.substring(key.length, titleKey.length); 
						}
					  });
                      var key = titleKey;
                      var value = 1;
                      emit( key, value );
                  },
    reduce : function(key, values) {

					var count = 0;
					values.forEach( function(value) {
										  count += value;
									}
								  );
					return count;
				 },
	scope:{kws:[{key:"回应: "},{key:"回复:"}]},
	out: { reduce: "travel_forum_group" },
	verbose:true
 }
);

猜你喜欢

转载自tydldd.iteye.com/blog/2146459