mongodbのmapReduce 簡易メモ

keyを集計して件数を取得


mapreduce.js

var map = function() {
	emit(
		this.key, 	// key
		1		// value
	);
}

var reduce = function(key, values) {
	var count = 0;
	values.forEach(function(value) {
		count += value;
	});
	return count;
}

var mongo = new Mongo('localhost');
var db = mongo.getDB('db');
var res = db.collection.mapReduce(map, reduce, {out: "tmp_out_collection"});

shellPrint(res);


実行

mongo mapreduce.js