Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8857243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:32:37+00:00 2026-06-14T14:32:37+00:00

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

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

    o.reduce = function(_customer, values){
        var result = {_customer: _customer, sum_points: 0, };
        values.forEach(function(point){
            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){

            log(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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:32:38+00:00Added an answer on June 14, 2026 at 2:32 pm

    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;
            values.forEach(function(point){
                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:

    db.createdCollectionNameForResults.find().sort({value:-1}).limit(5)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.