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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:11:29+00:00 2026-05-26T09:11:29+00:00

I am new to map / reduce and trying to figure out a way

  • 0

I am new to map / reduce and trying to figure out a way to collect the following data using map / reduce instead doing it my my (slow) application logic:

I have a collection ‘projects’ with a 1:n relation to a collection ‘tasks’. Now I’d like to receive an array of results that gives me the project names where the first is the project with the most tasks, and the last the project with the least tasks.

Or even better an array of hashes that also tells me how many tasks every project has (assuming the project name is unique:

[project_1: 23, project_2: 42, project_3: 82]

For map I tried something like:

map = function () {
  emit(this.project_id, { count:1 });
}

and reduce:

reduce = function (key, values) {
  var sum = 0;
  values.forEach(function(doc){ sum += 1; });
  return { count:sum };
}

I fired this against my tasks collection:

var mr = db.tasks.mapReduce(map, reduce, { out: "results" });

But I get crucial results when querying:

db[mr.result].find();

I am using Mongoid on Rails and am completely lost with it. Can someone point me into the right direction?

Thx in advance.
Felix

  • 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-26T09:11:30+00:00Added an answer on May 26, 2026 at 9:11 am

    Looks generally right, but I spot at least one problem: The summation step in the reduce function should be

      values.forEach(function(doc){ sum += doc.count ; });
    

    because the function may be reducing values that are themselves the product of a prior reduction step, and that therefore have count values > 1.

    That’s a common oversight, mentioned here: http://www.mongodb.org/display/DOCS/Troubleshooting+MapReduce

    Hope that helps!

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

Sidebar

Related Questions

I am new to Hadoop and I am trying to figure out a way
I'm using the new Hadoop API to write a sequence of map-reduce jobs. I
I have been trying to run simple map-reduce jobs on data stored in Cassandra
I'm new to Hadoop and I'm trying to figure out how it works. As
I'm new to google map. I'm developing an application about showing cars on google
I wrote a simple map reduce job that would read in data from the
I am trying to perform queries using the OR operator as following: MapReduceResult result
So my friend and I are trying to do a map reduce on a
I am very new for Hadoop and Map Reduce. For starting bases i executed
I have created the following Map Reduce and came across something curious. I'm counting

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.