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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:23:06+00:00 2026-06-06T14:23:06+00:00

I have several Mongoid models that I’m running mapreduce on, and I’d like to

  • 0

I have several Mongoid models that I’m running mapreduce on, and I’d like to store the unified results in a single daily_stats collection. My map & reduce functions work fine for all 3 models, but even when outputting via collection.mapreduce(map, reduce, {:out => "daily_stats", :raw => true}), the results of subsequent map reduce operations overwrite previous results, even it they don’t have overlapping keys:

{'_id': "2012-06-01", 'values': {photos: 10}}
{'_id': "2012-06-02", 'values': {photos: 10}}

Values for photos get thrown out when a subsequent pass returns:

{'_id': "2012-06-01", 'values': {comments: 1}}
{'_id': "2012-06-02", 'values': {comments: 6}}

I tried merging also with collection.mapreduce(map, reduce, {:out => {:merge => "daily_stats"}, :raw => true}), but that doesn’t seem to work either.

Any ideas?

UPDATE

The map & reduce functions are like this for each model:

Map:

function() {
    day = Date.UTC(this.created_at.getFullYear(), this.created_at.getMonth(), this.created_at.getDate());
    emit(day, {users: 1});
  };

Reduce:
function(key, values) {
var users_added_count = 0;

    values.forEach(function(v) {
      users_added_count += parseInt(v['users']) || 0;
    });

    return {users: users_added_count};
  }

Here’s some extra info about the resulting schema:

{ "_id" : 1337040000000,
 "value" : {
  "apartments" : 280,
  "price" : 1003653,
  "photos" : 83,
  "comments" : 0 } 
 }
  • 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-06T14:23:08+00:00Added an answer on June 6, 2026 at 2:23 pm

    If you look at the MongoDB documentation for map reduce (http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-Outputoptions), you’ll see that by default, the MR output collection replaces any existing collections with same name. “Merge” adds new data into the old output collection, but overwrites documents with the same key.

    It looks like your key is the date? If

    {'_id': "2012-06-01", 'values': {photos: 10}}
    

    and

    {'_id': "2012-06-01", 'values': {comments: 1}}
    

    have the same key, the second document will replace the first when you run MR. You either need to specify a more unique key, or you need to have multiple output collections (perhaps one for photos and one for comments?).

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

Sidebar

Related Questions

I'm running Rails 3, and I've got several controllers that all do something like
I have several old 3.5in floppy disks that I would like to backup. My
I have several complex data structures like Map< A, Set< B > > Set<
i have several classes with members called 'Id'. Originally i wanted to store these
I have a lot of optional fields in Mongoid, like: field :key, type: String
I have several LINQ queries that will churn records (up to a million) based
I have several hex-values that I try to write to a file. It seems
I have several gigabytes of documents which I wish to store online somewhere in
I have several div's that have their html modified during the life of the
I have several different user controls that get built dynamically and added to a

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.