Archive

node.js mongoDb group by count

I am writing an app in node.js and starting to do some advanced data manipulation now. One thing I have not found in the mongoDb docs was how to do group by count query.

So here is my solution. (nodejs code right out of my array utility "class")

// Arry to object with defined keys.
exports.groupCount = function(array, key){
	
	var group = {};
	
	array.forEach(function(d, i){
		
		var keyName = d[key];
		
		if(group[keyName]){
			group[keyName].count++;
		} else {
			group[keyName] = {
				count: 1
			}
		}
	});

	return group;
};

Comments: