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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:14:01+00:00 2026-06-10T09:14:01+00:00

I might be a bit in over my head on this as I’m still

  • 0

I might be a bit in over my head on this as I’m still learning the ins and outs of MongoDB, but here goes.

Right now I’m working on a tool to search/filter through a dataset, sort it by an arbitrary datapoint (eg. popularity) and then group it by an id. The only way I see I can do this is through Mongo’s MapReduce functionality.

I can’t use .group() because I’m working with more than 10,000 keys and I also need to be able to sort the dataset.

My MapReduce code is working just fine, except for one thing: sorting. Sorting just doesn’t want to work at all.

db.runCommand({
  'mapreduce': 'products',
  'map': function() {
    emit({
      product_id: this.product_id,
      popularity: this.popularity
    }, 1);
  },
  'reduce': function(key, values) {
    var sum = 0;
    values.forEach(function(v) {
      sum += v;
    });

    return sum;  
  },
  'query': {category_id: 20},
  'out': {inline: 1},
  'sort': {popularity: -1}
});

I already have a descending index on the popularity datapoint, so it’s definitely not working because of a lack of that:

{ 
  "v" : 1, 
  "key" : { "popularity" : -1 }, 
  "ns" : "app.products", 
  "name" : "popularity_-1" 
}

I just cannot figure out why it doesn’t want to sort.

Instead of inlining the result set, I can’t output it to another collection and then run a .find().sort({popularity: -1}) on that because of the way this feature is going to work.

  • 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-10T09:14:02+00:00Added an answer on June 10, 2026 at 9:14 am

    First of all, Mongo map/reduce are not designed to be used in as a query tool (as it is in CouchDB), it is design for you to run background tasks. I use it at work to analyze traffic data.

    What you are doing wrong however is that you’re applying the sort() to your input, but it is useless because when the map() stage is done the intermediate documents are sorted by each keys. Because your key is a document, it is being sort by product_id, popularity.

    This is how I generated my dataset

    function generate_dummy_data() {
        for (i=2; i < 1000000; i++) { 
            db.foobar.save({
              _id: i, 
             category_id: parseInt(Math.random() * 30), 
             popularity:    parseInt(Math.random() * 50)
            }) 
        }
    }
    

    And this my map/reduce task:

    var data = db.runCommand({
      'mapreduce': 'foobar',
      'map': function() {
        emit({
          sorting: this.popularity * -1,
          product_id: this._id,
          popularity: this.popularity,
        }, 1);
      },
      'reduce': function(key, values) {
        var sum = 0;
        values.forEach(function(v) {
          sum += v;
        });
    
        return sum;  
      },
      'query': {category_id: 20},
      'out': {inline: 1},
    });
    

    And this is the end result (very long to paste it here):

    http://cesarodas.com/results.txt

    This works because now we’re sorting by sorting, product_id, popularity. You can play with the sorting how ever you like just remember that the final sorting is by key regardless of you how your input is sorted.

    Anyway as I said before you should avoid doing queries with Map/Reduce it was designed for background processing. If I were you I would design my data in such a way I could access it with simple queries, there is always a trade-off in this case complex insert/updates to have simple queries (that’s how I see MongoDB).

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

Sidebar

Related Questions

I think my question might seem a bit odd, but here it goes; I'm
I might be being a bit thicky here but please answer me this. Consider
This might sound a bit of an odd question but I know what I
This might seem a bit weird, but since I am new in RoR and
Ok this might be a bit of hack but bear with me :) The
I know this might be a bit awkward but I am trying to modify
Might be a bit of noob question but it's something that's been getting me
My theory might be a bit weak asking this, so feel free to let
The question might seem a bit confusing but I have an if statement that
My wording might be a bit off, but I wonder if there's an open-source

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.