Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
mapReduce with day_of_week, month_of_year
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Brad Karels  
View profile  
 More options Jun 21 2012, 11:57 am
From: Brad Karels <bkar...@gmail.com>
Date: Thu, 21 Jun 2012 08:57:28 -0700 (PDT)
Local: Thurs, Jun 21 2012 11:57 am
Subject: mapReduce with day_of_week, month_of_year

I am new to mongo and not a JavaScript ninja so forgive my silliness.  That
said, I am looking to perform a mapreduce on a set of events to get counts
by day of the week (i.e. What day of the week is most popular for events?)
 My efforts have left me short around how to extract the day of week (or
month of year) from a date inside the mapReduce function.  My solution thus
far looks as follows:

I have been poking around here and other places a bit and have found date
operations like this seem to yet be a bit limiting.  So if someone can put
me down a more correct path I'd appreciate any advice moving forward.

Thank you.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Scott Hernandez  
View profile  
 More options Jun 21 2012, 1:06 pm
From: Scott Hernandez <scotthernan...@gmail.com>
Date: Thu, 21 Jun 2012 18:06:48 +0100
Local: Thurs, Jun 21 2012 1:06 pm
Subject: Re: [mongodb-user] mapReduce with day_of_week, month_of_year
You are close.

Here is a reference for the javascript Date object:
http://www.w3schools.com/jsref/jsref_obj_date.asp

Here is an example in the shell:

> var now = new Date()
> now.getDay()
4
> now.getMonth()

5

Remember these are 0 based.

Also, don't forget to copy the date to your retVal object as you are
creating a new Date here:
var retVal = { count:0, date:new Date(), su:0, m:0, t:0, w:0, r:0, f:0, sa:0 };


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
William Z  
View profile  
 More options Jun 21 2012, 3:00 pm
From: William Z <william.z...@10gen.com>
Date: Thu, 21 Jun 2012 12:00:51 -0700 (PDT)
Local: Thurs, Jun 21 2012 3:00 pm
Subject: Re: mapReduce with day_of_week, month_of_year

Hi Brad!

There seem to be a couple of problems with your map/reduce process, beyond
just your problems with the getDay() function.  

1) For good help in debugging map/reduce (specifically, in debugging the
output of the map() function), check out this page:
http://www.mongodb.org/display/DOCS/Troubleshooting+MapReduce

2) I think that you should be checking for the specific day of the week in
your map() function, and not in the reduce() function.  Remember, the
reduce() function is only used to accumulate what's been emitted by the
map() function.

Here is the code that I used to get the correct results for your problem:

map = function() {
    var day = this.date.getDay();
    var ret = { count: 1, su:0, mo:0, tu:0, we:0, th:0, fr:0, sa:0, };
    if ( day == 0 ) ret.su++;
    if ( day == 1 ) ret.mo++;
    if ( day == 2 ) ret.tu++;
    if ( day == 3 ) ret.we++;
    if ( day == 4 ) ret.th++;
    if ( day == 5 ) ret.fr++;
    if ( day == 6 ) ret.sa++;
    emit({ event: this.event }, ret );

}

reduce = function(key, values) {
  var ret = { count:0, su:0, mo:0, tu:0, we:0, th:0, fr:0, sa:0, }

  values.forEach(function(v) {
    ret.count += v.count;
    ret.su += v.su;
    ret.mo += v.mo;
    ret.tu += v.tu;
    ret.we += v.we;
    ret.th += v.th;
    ret.fr += v.fr;
    ret.sa += v.sa;
  });

  return ret ;

};

I hope this helps!

 -William


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brad Karels  
View profile  
 More options Jun 22 2012, 7:56 am
From: Brad Karels <bkar...@gmail.com>
Date: Fri, 22 Jun 2012 04:56:10 -0700 (PDT)
Local: Fri, Jun 22 2012 7:56 am
Subject: Re: mapReduce with day_of_week, month_of_year

Thank you Scott and William - this adds clarity and got me over this hump!

I am still curious, however, as to why date.getDay() inside the reduce
function returns "day of month" (e.g. 18 for 18-6-2012).  To William's
point, this should be unnecessary, but it's still bugging me a bit.

Thank you both for your input - I appreciate your help.

Brad


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »