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

  • Home
  • SEARCH
  • 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 1103865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:24:07+00:00 2026-05-17T01:24:07+00:00

First, the background. I used to have a collection logs and used map/reduce to

  • 0

First, the background. I used to have a collection logs and used map/reduce to generate various reports. Most of these reports were based on data from within a single day, so I always had a condition d: SOME_DATE. When the logs collection grew extremely big, inserting became extremely slow (slower than the app we were monitoring was generating logs), even after dropping lots of indexes. So we decided to have each day’s data in a separate collection – logs_YYYY-mm-dd – that way indexes are smaller, and we don’t even need an index on date. This is cool since most reports (thus map/reduce) are on daily data. However, we have a report where we need to cover multiple days.

And now the question. Is there a way to run a map/reduce (or more precisely, the map) over multiple collections as if it were only one?

  • 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-17T01:24:08+00:00Added an answer on May 17, 2026 at 1:24 am

    A reduce function may be called once, with a key and all corresponding values (but only if there are multiple values for the key – it won’t be called at all if there’s only 1 value for the key).

    It may also be called multiple times, each time with a key and only a subset of the corresponding values, and the previous reduce results for that key. This scenario is called a re-reduce. In order to support re-reduces, your reduce function should be idempotent.

    There are two key features in a idempotent reduce function:

    • The return value of the reduce function should be in the same format as the values it takes in. So, if your reduce function accepts an array of strings, the function should return a string. If it accepts objects with several properties, it should return an object containing those same properties. This ensures that the function doesn’t break when it is called with the result of a previous reduce.
    • Don’t make assumptions based on the number of values it takes in. It isn’t guaranteed that the values parameter contains all the values for the given key. So using values.length in calculations is very risky and should be avoided.

    Update: The two steps below aren’t required (or even possible, I haven’t checked) on the more recent MongoDB releases. It can now handle these steps for you, if you specify an output collection in the map-reduce options:

    { out: { reduce: "tempResult" } }
    

    If your reduce function is idempotent, you shouldn’t have any problems map-reducing multiple collections. Just re-reduce the results of each collection:

    Step 1

    Run the map-reduce on each required collection and save the results in a single, temporary collection. You can store the results using a finalize function:

    finalize = function (key, value) {
      db.tempResult.save({ _id: key, value: value });
    }
    
    db.someCollection.mapReduce(map, reduce, { finalize: finalize })
    db.anotherCollection.mapReduce(map, reduce, { finalize: finalize })
    

    Step 2

    Run another map-reduce on the temporary collection, using the same reduce function. The map function is a simple function that selects the keys and values from the temporary collection:

    map = function () {
      emit(this._id, this.value);
    }
    
    db.tempResult.mapReduce(map, reduce)
    

    This second map-reduce is basically a re-reduce and should give you the results you need.

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

Sidebar

Related Questions

First some background on the questions. I have never used ravedb before and I'm
First a little background: I have an Event model that has various event_type s.
I'm using jQuery validation plugin. I have two images: first background of label, when
Some setup background first: I have a cronjob which runs a PHP file called
Hi and thanks for your attention. First some background on the question: I have
First a little background: I have already managed to connect to a Microsoft SOAP
First, a little background. I have strings that resemble the following: ((Foo.Bar.StartsWith(A)) && (Foo.Bar.EndsWith(B)))
First some background: The company I work for have decided to create an iPhone
First some background: I already have a Result class that I've implemented to carry
First some background. I have a batch-type java process run from a DOS batch

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.