Comparing dates inside of mapreduce

217 views
Skip to first unread message

Vegar Vikan

unread,
Apr 25, 2016, 5:06:12 PM4/25/16
to mongodb-user
I posted this question on stackoverflow, but it didn't get any attention, so I try here as well...

I have a map function in a mapreduce query where I try to compare a couple of dates.

Lets say that the documents contains a property 'Created' with type ISODate:

{
  Created: ISODate("2016-01-01T12:00:00.000+0000")
}

In my map/reduce method, I pass a date through the scope:

{
   start: ISODate("2015-01-01T12:00:00.000+0000")
}

and in my map method, I print out the following

function() {
   print(this.Created);
   print(start);
   print(this.Created > start);
}

I'll get the following output in my log:

2016-04-25T16:19:04.921+0200 I -        [conn129] ISODate("2016-01-01T12:00:00.000+0000")
2016-04-25T16:19:04.922+0200 I -        [conn129] Sat Jan 01 2015 13:00:00 GMT+0100 (Vest-Europa (normaltid)) 
2016-04-25T16:19:04.922+0200 I -        [conn129] false 

Why is the dates printed differently?
How can I compare the start-date with the created date?

regars,
-Vegar

Asya Kamsky

unread,
Apr 25, 2016, 8:37:34 PM4/25/16
to mongodb-user
Are you sure you want to be using mapReduce? Most things that people
do in mapReduce can be done faster in aggregation framework. Can you
give a bit more context for your problem? Are you emitting different
values depending on relationship between "created" and "start"?

Asya
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user"
> group.
>
> For other MongoDB technical support options, see:
> https://docs.mongodb.org/manual/support/
> ---
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mongodb-user...@googlegroups.com.
> To post to this group, send email to mongod...@googlegroups.com.
> Visit this group at https://groups.google.com/group/mongodb-user.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mongodb-user/9fc028b4-3f48-4dbb-b76b-9b017eec9adc%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Asya Kamsky
Lead Product Manager
MongoDB
Download MongoDB - mongodb.org/downloads
Free MongoDB Monitoring - cloud.mongodb.com
Free Online Education - university.mongodb.com
Get Involved - mongodb.org/community
We're Hiring! - https://www.mongodb.com/careers

Vegar Vikan

unread,
Apr 26, 2016, 2:53:45 AM4/26/16
to mongodb-user
No, I'm not sure I want to use mapreduce, but that's the solution I have found.

The data structure is not the best I guess....


The document has a structure similar to

{
   
CurrentThing: {
       
StartDate: ISODate(".....")
   
},
   
HistoryOfThings: [
       
{
             
StartDate: ISODate("...."),
             
EndDate: ISODate("....")
       
},
       
{
             
StartDate: ISODate("...."),
             
EndDate: ISODate("....")
       
}
   
]

}


Some events leads to the 'CurrentThing' being moved to the 'HistoryOfThings' and the 'CurrentThing' being initialized with a new thing.

Now, I need a list of every 'Thing' for a given period, grouped on a property common across different documents.

Aggregation would be my first guess, but even though I can $unwind the 'HistoryOfThings'-array, I'm not sure how to handle the 'CurrentThing'-subdocument.

So I ended up doing map reduce, and want to emit for each 'Thing' according to the daterange given.

Any other suggestions is welcome, but I'm still curious why comparing dates didn't go as expected :-)

Vegar Vikan

unread,
Apr 26, 2016, 5:41:00 AM4/26/16
to mongodb-user
Wow - I think I fund the issue.

Visual Studio decided to 'clean up' my sample data file, changing ISODate("...") into "ISODate(\"....\")".

So what I have believed is ISODate fields is actually string fields....


Anyway - would love to get some input on alternatives.

Asya Kamsky

unread,
Apr 27, 2016, 5:01:55 PM4/27/16
to mongodb-user
Well, that would certainly explain why comparison isn't returning the
expected result.

As far as doing aggregation here, I think you just need to do
appropriate $match of dates being within range. Probably you will
have some complex $or predicate if you need to compare both current
and historical date ranges.

Asya
> https://groups.google.com/d/msgid/mongodb-user/1bcf45b2-90e1-44c6-a541-a508b2e2ec7e%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages