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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:16:12+00:00 2026-05-27T15:16:12+00:00

I have data which is organized hierarchically and I’d like to compute aggregations at

  • 0

I have data which is organized hierarchically and I’d like to compute aggregations at several levels within a single mongodb map/reduce operation. Is there a way to do this?

Example:

{ street: "A", district: "1", city: "Z", nb_users: 1 }
{ street: "A", district: "1", city: "Z", nb_users: 2 }
{ street: "B", district: "1", city: "Z", nb_users: 3 }
{ street: "B", district: "1", city: "Z", nb_users: 2 }
{ street: "C", district: "1", city: "Z", nb_users: 4 }
{ street: "C", district: "1", city: "Z", nb_users: 3 }
{ street: "A", district: "2", city: "Z", nb_users: 5 }
{ street: "B", district: "2", city: "Z", nb_users: 6 }
{ street: "B", district: "2", city: "Z", nb_users: 3 }

Result:

{ street: "A", district: "1", city: "Z", nb_users_street: 3, nb_users_district: 15, nb_users_city: 29 }
{ street: "B", district: "1", city: "Z", nb_users_street: 5, nb_users_district: 15, nb_users_city: 29 }
{ street: "C", district: "1", city: "Z", nb_users_street: 7, nb_users_district: 15, nb_users_city: 29 }
{ street: "A", district: "2", city: "Z", nb_users_street: 5, nb_users_district: 14, nb_users_city: 29 }
{ street: "B", district: "2", city: "Z", nb_users_street: 9, nb_users_district: 14, nb_users_city: 29 }

Thanks for your help!

  • 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-27T15:16:12+00:00Added an answer on May 27, 2026 at 3:16 pm

    No, there is no easy way to do this.

    As you are wanting to aggregate by street, district and city you will need to use all of them as part of the key of your emitted objects, so your map function would most probably look something like this:

    function(){
      emit ( 
        { street : this.street, district : this.district, city : this.city }, 
        {nb_users : this.nb_users } 
      );
    } 
    

    As the reduce function combines only records with matching keys, you will only be able to combine records where both the street, district and city are the same – which means you won’t be able to calculate the total for a district or city from these emitted objects as they span multiple streets.

    Doing three separate map/reduces into three separate output collections will make the code simpler and easier to understand, and will also remove the redundancy of having nb_users_district and nb_users_city repeated for every street level row.

    In fact, the three separate map/reduce functions would be so simple that you should be able to use MongoDB’s built in group function, which I believe offers some performance benefits over standard map/reduce.

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

Sidebar

Related Questions

I have some data which look like that: PMID- 19587274 OWN - NLM DP
I have a C++ class representing a hierarchically organised data tree which is very
I have an excel sheet which has some huge data. Data is organized as
I'm implementing several classes which does not have data by itself, just logics. These
I have some data in MongoDB that looks like this: { name: Steve, location:
I have a set of data that's organized hierarchically that should be able to
I have data which resembles the following: D.STEIN,DS,01,ALTRES,TTTTTTFFTT D.STEIN,DS,01,APCASH,TTTTTTFFTT D.STEIN,DS,01,APINH,TTTTTTFFTT D.STEIN,DS,01,APINV,TTTTTTFFTT D.STEIN,DS,01,APMISC,TTTTTTFFTT D.STEIN,DS,01,APPCHK,TTTTTTFFTT D.STEIN,DS,01,APWLNK,TTTTTTFFTT
I have data which resembles the following: D.STEIN,DS,01,ALTRES,TTTTTTFFTT D.STEIN,DS,01,APCASH,TTTTTTFFTT D.STEIN,DS,01,APINH,TTTTTTFFTT D.STEIN,DS,01,APINV,TTTTTTFFTT D.STEIN,DS,01,APMISC,TTTTTTFFTT D.STEIN,DS,01,APPCHK,TTTTTTFFTT D.STEIN,DS,01,APWLNK,TTTTTTFFTT
I have some data (which users input using WYSIWYG editor). I have created a
I have saved data which was saved using NSUserDefaults. I was under the impression

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.