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 8283719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:50:25+00:00 2026-06-08T10:50:25+00:00

I am looking for a way to generate some summary statistics using Mongo. Suppose

  • 0

I am looking for a way to generate some summary statistics using Mongo. Suppose I have a collection with many records of the form

{"name" : "Jeroen", "gender" : "m", "age" :27.53 }

Now I want to get the distributions for gender and age. Assume for gender, there are only values "m" and "f". What is the most efficient way of getting the total count of males and females in my collection?

And for age, is there a way that does some ‘binning’ and gives me a histogram like summary; i.e. the number of records where age is in the intervals: [0, 2), [2, 4), [4, 6) ... etc?

  • 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-08T10:50:28+00:00Added an answer on June 8, 2026 at 10:50 am

    Konstantin’s answer was right. MapReduce gets the job done. Here is the full solution in case others find this interesting.

    To count genders, the map function key is the this.gender attribute for every record. The reduce function then simply adds them up:

    // count genders
    db.persons.mapReduce(
        function(){
            emit(this["gender"], {count: 1})
        }, function(key, values){
            var result = {count: 0};
            values.forEach(function(value) {
                result.count += value.count;
            });
            return result;
        }, {out: { inline : 1}}
    );
    

    To do the binning, we set the key in the map function to round down to the nearest division by two. Therefore e.g. any value between 10 and 11.9999 will get the same key "10-12". And then again we simply add them up:

    db.responses.mapReduce(
        function(){
            var x = Math.floor(this["age"]/2)*2;
            var key = x + "-" + (x+2);
            emit(key, {count: 1})
        }, function(state, values){
            var result = {count: 0};
            values.forEach(function(value) {
                result.count += value.count;
            });
            return result;
        }, {out: { inline : 1}}
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a very easy and quick way to generate some SAML assertions.
I'm looking for a more efficient way to generate some DOM changes and update
I'm looking to generate some statistics about a model I created in python. I'd
I am looking for some advice as to the best way to generate a
I am looking for a way to generate pseudo random number sequences that will
I'm looking for a way to programatically generate a twitter feed for a .NET
I was looking for a flexible way to generate different sidebars in different pages.
I'm looking for an easy way to generate previews for labels generated as pdfs.
I'm looking for an elegant way to generate a sequence of the rolling average
I am looking for a concise way to generate an array of integers 1

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.