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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:30:37+00:00 2026-05-20T15:30:37+00:00

I still don’t fully understand how map/reduce works, so I thought I’d give an

  • 0

I still don’t fully understand how map/reduce works, so I thought I’d give an example of a problem I need to solve, and hopefully the answer will help me understand the concept.

I’m tracking page views using a document structure similar to this:

{
    "timestamp" : 1299990045,
    "visitor" : {
        "region" : {
            "country_code" : "US",
        },
        "browser" : {
            "name" : "IE",
            "version" : "8.0",
        }
    },
    "referer" : {
        "host" : "www.google.com",
        "path" : "/",
        "query" : "q=map%2Freduce"
    }
}

I store a single document for each page view. Because I get about 15 million page views a day, I’d like to aggregate these results each night, save the aggregate results for that day, and then drop the collection to begin storing page views again. I want the output of the map/reduce to look like this:

{
    "day" : "Sun Mar 13 2011 00:00:00 GMT-0400 (EDT)",
    "regions" : {
        "US" : 235,
        "CA" : 212,
        "JP" : 121
    },
    "browsers" : {
        "IE" : 145,
        "Firefox" : 245,
        "Chrome" : 95,
        "Other" : 120
    },
    "referers" : {
        "www.google.com" : 24,
        "yahoo.com" 56
    }
}

I really don’t know where to begin doing this kind of thing. Any help would be appreciated.

  • 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-20T15:30:38+00:00Added an answer on May 20, 2026 at 3:30 pm

    The typical process for writing a map-reduce job is to start with the data format you want as the output of your reduce, build a map function that outputs it, then a reduce function that adds them up. In your example, you’d do something like this:

    function map() { 
        var date = new Date( this.timestamp.getFullYear(), 
                             this.timestamp.getMonth(),
                             this.timestamp.getDay() );
        var out  = { regions: {}, browsers: {}, referers: {} };
        out.regions[ this.visitor.region.country_code ] = 1; 
        out.browsers[ this.visitor.browser.name ] = 1;
        out.referers[ this.referer.host ] = 1; 
        emit( date, out);
    }
    
    function reduce( key, values ) { 
        var out = { regions: {}, browsers: {}, referers: {} };
        values.forEach(function(value) { 
          for( var region in value.regions ) { 
            if( out.regions[region] ) { 
              out.regions[ region ] += value[region];
            } else { 
              out.regions[ region ] = value[region];
            }
          };
          for( var browser in value.browsers ) { 
            if( out.browsers[browser] ) { 
              out.browsers[ browser ] += value[browser]; 
            } else { 
              out.browsers[ browser ] = value[browser]; 
            }
          };
          for( var referer in value.referers ) { 
            if( out.referers[ referer] ) { 
              out.referers[ referer ] += value[referer];
            } else {  
              out.referers[ referer ] = value[referer];
            } 
          }
        });
        return out; 
    } 
    

    At the end of this, you should have an output collection that looks something like this:

    { 
      _id: "Sun Mar 13 2011 12:23:58 GMT-0700 (PDT)", 
      value: { 
        regions:  {
          "US" : 235,
          "CA" : 212,
          "JP" : 121
        },
        browsers: { 
          "IE" : 145,
          "Firefox" : 245,
          "Chrome" : 95,
          "Other" : 120 
        }, 
        referers: { 
          "www.google.com" : 24,
          "yahoo.com" 56
        } 
      } 
    }
    

    Note that there’s another way you can do this.. Rather than doing a map reduce job, you can also keep all of this data in real time using atomic increments and upserts.

    For example, each time you generate one of your page view docs, you could also do an update like this:

    db.pageviews.summaries.update( { _id: new Date( this.timestamp.getFullYear(), 
                                                    this.timestamp.getMonth(),
                                                    this.timestamp.getDay() ) },
                                   { $inc : {
                                         'visitor.region.US' : 1,
                                         'visitor.browser.IE' : 1,
                                         'referer.www.google.com' : 1 
                                      }
                                   },
                                   true // upsert
                                 );
    

    This would mean that you always have your summary document up to date and don’t need any map reduce jobs.

    Note, you might want to escape the ‘.’ in your domain names as Mongo will interpret that as a hierarchy of documents rather than an attribute name.

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

Sidebar

Related Questions

I read the JVM specification for the fpstrict modifier but still don't fully understand
I'm learning jQuery but I still don't fully undestand how it works. Suppose I
I searched through whole internet but I still don't understand how I need to
I read several posts regarding this but still don't understand how to solve my
Something I still don't understand when performing an http-get request to the server is
Been looking through other answers and I still don't understand the modulo for negative
I already read the datasheet and google but I still don't understand something. In
I've looked at sites and MSDN documentation but I still don't understand the last
There are some things that I still don't really understand with pointers when you
Could somebody please elaborate this paragraph to me: If you still don’t understand how

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.