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 };
On Thu, Jun 21, 2012 at 4:57 PM, Brad Karels <
bka...@gmail.com> wrote:
> 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:
>>>
>>> /* mongo localhost:27017/tracking countByDayOfWeek.js --shell */
>>>
>>>
>>> mapFunction=function() {
>>>
>>> emit( this.source, { count:1, date:this.dateCreated, su:1, m:1, t:1,
>>> w:1, r:1, f:1, sa:1 } );
>>>
>>> }
>>>
>>>
>>>
>>> reduceFunction=function(k,vals) {
>>>
>>> var retVal = { count:0, date:new Date(), su:0, m:0, t:0, w:0, r:0,
>>> f:0, sa:0 };
>>>
>>>
>>>
>>> vals.forEach(function(value) {
>>>
>>> retVal.count += value.count;
>>>
>>> var day = value.date.getDay; // Here seems to be the sticky bit
>>>
>>> if (day == 0) {
>>>
>>> retVal.su +=
value.su;
>>>
>>> }...{
>>>
>>> } else {
>>>
>>> retVal.sa +=
value.sa;
>>>
>>> }
>>>
>>> });
>>>
>>> return retVal;
>>>
>>> }
>>>
>>>
>>>
>>> db.runCommand({
>>>
>>> mapReduce: "track_event",
>>>
>>> map: mapFunction,
>>>
>>> reduce: reduceFunction,
>>>
>>> out: "myOutput"
>>>
>>> });
>
>
> 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 received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to
mongod...@googlegroups.com
> To unsubscribe from this group, send email to
>
mongodb-user...@googlegroups.com
> See also the IRC channel --
freenode.net#mongodb