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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:23:58+00:00 2026-05-25T06:23:58+00:00

the map reduce examples I see use aggregation functions like count, but what is

  • 0

the map reduce examples I see use aggregation functions like count, but what is the best way to get say the top 3 items in each category using map reduce.

I’m assuming I can also use the group function but was curious since they state sharded environments cannot use group(). However, I’m actually interested in seeing a group() example as well.

  • 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-25T06:23:58+00:00Added an answer on May 25, 2026 at 6:23 am

    For the sake of simplification, I’ll assume you have documents of the form:

    {category: <int>, score: <int>}
    

    I’ve created 1000 documents covering 100 categories with:

    for (var i=0; i<1000; i++) {
      db.foo.save({
        category: parseInt(Math.random() * 100),
        score: parseInt(Math.random() * 100)
      });
    }
    

    Our mapper is pretty simple, just emit the category as key, and an object containing an array of scores as the value:

    mapper = function () {
      emit(this.category, {top:[this.score]});
    }
    

    MongoDB’s reducer cannot return an array, and the reducer’s output must be of the same type as the values we emit, so we must wrap it in an object. We need an array of scores, as this will let our reducer compute the top 3 scores:

    reducer = function (key, values) {
      var scores = [];
      values.forEach(
        function (obj) {
          obj.top.forEach(
            function (score) {
              scores[scores.length] = score;
          });
      });
      scores.sort();
      scores.reverse();
      return {top:scores.slice(0, 3)};
    }
    

    Finally, invoke the map-reduce:

    db.foo.mapReduce(mapper, reducer, "top_foos");
    

    Now we have a collection containing one document per category, and the top 3 scores across all documents from foo in that category:

    { "_id" : 0, "value" : { "top" : [ 93, 89, 86 ] } }
    { "_id" : 1, "value" : { "top" : [ 82, 65, 6 ] } }
    

    (Your exact values may vary if you used the same Math.random() data generator as I have above)

    You can now use this to query foo for the actual documents having those top scores:

    function find_top_scores(categories) {
      var query = [];
      db.top_foos.find({_id:{$in:categories}}).forEach(
        function (topscores) {
          query[query.length] = {
            category:topscores._id,
            score:{$in:topscores.value.top}
          };
      });
      return db.foo.find({$or:query});
    

    }

    This code won’t handle ties, or rather, if ties exist, more than 3 documents might be returned in the final cursor produced by find_top_scores.

    The solution using group would be somewhat similar, though the reducer will only have to consider two documents at a time, rather than an array of scores for the key.

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

Sidebar

Related Questions

I know about map/reduce alghoritm and its use. It's using functions that are called
Map Reduce is a pattern that seems to get a lot of traction lately
Is it true that Map/Reduce results can be stored permanently, but not sorted? For
I would like to understand what is the best way to mitigate risk of
What is the easiest to use distributed map reduce programming system? For example. in
Got these examples I would like to understand but they are in Scheme. I
I hear a lot about map/reduce, especially in the context of Google's massively parallel
I used hadoop to run map-reduce applications on our cluster. The jobs take around
I am looking for a map/reduce function to calculate the status in a Design
I am trying to parallel a classic map-reduce problem (which can parallel well with

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.