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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:07:05+00:00 2026-05-26T02:07:05+00:00

I have documents like this: { _id : someid, name : somename, action :

  • 0

I have documents like this:

{
        "_id" : "someid",
        "name" : "somename",
        "action" : "do something",
        "date" : ISODate("2011-08-19T09:00:00Z")
}

I want to map reduce them into something like this:

{
        "_id" : "someid",
        "value" : {
            "count" : 100,
            "name" : "somename",
            "action" : "do something",
            "date" : ISODate("2011-08-19T09:00:00Z")
            "firstEncounteredDate" : ISODate("2011-07-01T08:00:00Z")
        }
}

I want to group the map reduced documents by “name”, “action”, and “date”. But every document should has this “firstEncounteredDate” containing the earliest “date” (that is actually grouped by “name” and “action”).

If I group by name, action and date, firstEncounteredDate would always be date, that’s why I’d like to know if there’s any way to get “the earliest date” (grouped by “name”, and “action” from the entire document) while doing map-reduce.

How can I do this in map reduce?

Edit: more detail on firstEncounteredDate (courtesy to @beny23)

  • 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-26T02:07:06+00:00Added an answer on May 26, 2026 at 2:07 am

    Seems like a two-pass map-reduce would fit the bill, somewhat akin to this example: http://cookbook.mongodb.org/patterns/unique_items_map_reduce/

    In pass #1, group the original “name”x”action”x”date” documents by just “name” and “action”, collecting the various “date” values into a “dates” array during reduce. Use a ‘finalize’ function to find the minimum of the collected dates.

    Untested code:

    // phase i map function : 
    
    function () {
      emit( { "name": this.name, "action": this.action } , 
            { "count": 1, "dates": [ this.date ] } );
    }
    
    // phase i reduce function : 
    
    function( key, values ) {
      var result = { count: 0, dates: [ ] };
    
      values.forEach( function( value ) {
        result.count += value.count;
        result.dates = result.dates.concat( value.dates );
      }
    
      return result;
    }
    
    // phase i finalize function : 
    
    function( key, reduced_value ) {
      var earliest = new Date( Math.min.apply( Math, reduced_value.dates ) );
      reduced_value.firstEncounteredDate = earliest ;
      return reduced_value;
    }
    

    In pass #2, use the documents generated in pass #1 as input. For each “name”x”action” document, emit a new “name”x”action”x”date” document for each collected date, along with the now determined minimum date common to that “name”x”action” pair. Group by “name”x”action”x”date”, summing up the count for each individual date during reduce.

    Equally untested code:

    // phase ii map function : 
    
    function() {
      this.dates.forEach( function( d ) {
        emit( { "name": this.name, "action": this.action, "date" : d } ,
              { "count": 1, "firstEncounteredDate" : this.firstEncounteredDate } );
      }
    }
    
    // phase ii reduce function : 
    
    function( key, values ) {
      // note: value[i].firstEncounteredDate should all be identical, so ... 
      var result = { "count": 0, 
                     "firstEncounteredDate": values[0].firstEncounteredDate };
    
      values.forEach( function( value ) {
        result.count += value.count;
      }
    
      return result;
    }
    

    Pass #2 does not do a lot of heavy lifting, obviously — it’s mostly copying each document N times, one for each unique date. We could easily build a map of unique dates to their incidence counts during the reduce step of pass #1. (In fact, if we don’t do this, there’s no real point in having a “count” field in the values from pass #1.) But doing the second pass is a fairly effortless way of generating a full target collection containing the desired documents.

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

Sidebar

Related Questions

I have documents like this in my CouchDB: { _id: 0cb35be3cc73d6859c303fa3200011d2, _rev: 1-f6e356bbf6ab09290aae11132af50d66, adresse:
I have a Lucene Index containing documents like these : _id | Name |
I have stored images from the net like this Documents/imagecache/domain1/original/path/inURI/foo.png Documents/imagecache/domain2/original/path/inURI/bar.png Documents/imagecache/... Documents/imagecache/... Now
I have a ClickOnce environment like this: \\Fileserver\ClickOnceApps\App1.application C:\Documents and Settings\user\Start Menu\Programs\publisher\app1.appref-ms My understanding
I have a document, something like this: <root> <A node=1/> <B node=2/> <A node=3/>
I have an XML document which looks something like this: <meadinkent> <record> <comp_div>MENSWEAR</comp_div> <sty_ret_type>ACCESSORIES</sty_ret_type>
This is a longshot but I have a file with something like this: document
I have Two tables like this: Table categories: columns: id, name, parent 1, Foods,
I have one document in my params collection like this: { _id: ObjectId(4d124cef3ffcf6f410000037), code:
I have a MongoDB collection called employees where there are documents like this: {

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.