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

  • SEARCH
  • Home
  • 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 7595633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:43:09+00:00 2026-05-30T21:43:09+00:00

I want to use mapreduce to perform the group aggregation. Here is my map

  • 0

I want to use mapreduce to perform the group aggregation.
Here is my map function :

function() {
     emit(this.TransactionType, { Count: 1 });
}

Here is two reduce functions :

function(key, values) {
    var result = {Count: 0};
    values.forEach(function(value) {
        result.Count += 1; 
    });
    return result;
}

function(key, values) {
    var result = {Count: 0};
    values.forEach(function(value) {
        result.Count += value.Count; 
    });
    return result;
}

and here is the two results :

"_id" : "A", "value" : { "Count" : 13.0 }  
"_id" : "B", "value" : { "Count" : 2.0 }  
"_id" : "C", "value" : { "Count" : 1.0 }  
"_id" : "D", "value" : { "Count" : 209.0 }  
"_id" : "E", "value" : { "Count" : 66.0 }  
"_id" : "F", "value" : { "Count" : 11.0 }  
"_id" : "G", "value" : { "Count" : 17.0 }  
"_id" : "H", "value" : { "Count" : 17.0 } 


"_id" : "A", "value" : { "Count" : 128.0 } 
"_id" : "B", "value" : { "Count" : 115.0 } 
"_id" : "C", "value" : { "Count" : 1.0 } 
"_id" : "D", "value" : { "Count" : 3645.0 } 
"_id" : "E", "value" : { "Count" : 1405.0 } 
"_id" : "F", "value" : { "Count" : 256.0 } 
"_id" : "G", "value" : { "Count" : 380.0 } 
"_id" : "H", "value" : { "Count" : 398.0 } 

So why the two results are different?

Thank you very much

  • 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-05-30T21:43:10+00:00Added an answer on May 30, 2026 at 9:43 pm

    It’s helpful to think of the “reduce” function in terms of the “fold” higher-order function. That is to say, your “reduce” function will be applied to a list of values and an accumulated object (the “result” variable in your examples), which is initially specified but will eventually be replaced by the output of successive calls to your function. More over, the list of values to which your function will be applied can be broken up into any number of sub-lists, in any order!

    For example, consider how your function would behave using the JavaScript Array “reduce” function, which is an example of the “fold” higher-order function. Your first example will behave improperly because it doesn’t use the “Count” property of each element. Successive attempts to use it with Array#reduce will fail similarly:

    function badReducer(accum, x) {
      accum.Count += 1;
      return accum;
    }
    var sum = {Count:0};
    sum = [{Count:1}, {Count:2}, {Count:3}].reduce(badReducer, sum);
    sum; // => {Count:3}, d'oh!
    sum = [{Count:4}].reduce(badReducer, sum);
    sum; // => {Count:5}, d'oh!
    

    However, your second example properly adds the “Count” property and can be applied successively to its own output:

    function goodReducer(accum, x) {
      accum.Count += x.Count;
      return accum;
    }
    var sum = {Count:0};
    sum = [{Count:1}, {Count:2}, {Count:3}].reduce(goodReducer, sum);
    sum; // => {Count:6}, woohoo!
    sum = [{Count:4}].reduce(goodReducer, sum);
    sum; // => {Count:10}, woohoo!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use the Publish.GacRemove function to remove an assembly from GAC. However,
I have simple map-reduce type algorithm, which I want to implement in python and
i want to use title on text. is this possible to make title on
I'm working on Amazon's Elastic Map Reduce service and I want to bootstrap the
I want use $.ajax to read some infomation from xml file,here is my js
I have tried a simple Map/Reduce task using Amazon Elastic MapReduce and it took
I want use groovy findAll with my param to filtering closure filterClosure = {
i want use some data from a website with web service. i have a
I have a transaction log file in CSV format that I want use to
Below is my stored procedure. I want use stored procedure select all row of

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.