Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

how to populate map-reduced result

how to populate map-reduced result


TestSessionSchema.statics.getTopTesters = function(callback){
    var o = {}; = function(){
        emit(this._customer, this.points);

    o.reduce = function(_customer, values){
        var result = {_customer: _customer, sum_points: 0, };
            result.sum_points += point;
        return result;

    o.out = { replace: 'createdCollectionNameForResults' };
    o.verbose = true;

    this.mapReduce(o,function(err, model, stats){
        model.find().sort({'value.sum_points': "desc"}).limit(5).populate("_customer").exec(function(error, results){




In map-Reduce part I am geting _customers' points.

Now i want to populate _customer to get customers' info model.find().sort({'value.sum_points': "desc"}).limit(5).populate("_customer") does not work

how can I do it?

Problem courtesy of: hhs


When you do mapReduce in MongoDB, you must make sure that your emit value has the same format as your reduce return value.

That means in your case that if map calls emit(customer, points) then reduce must return simply the value for points.

To change your specific example, your map Function is okay, but your reduce function should be:

reduce = function(_customer, values) {
        var result =  0;
            result += point;
        return result;

Your reduce function would then sum up all of the values (points) for each _customer, which is what you want. The output would be _customer, point as key-value pairs with field names _id and value where value would be the sum of all the point values for that _id (_customer).

To query the result you would run:


Solution courtesy of: Asya Kamsky


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

how to populate map-reduced result


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription