Is there a way to take timezone into account when running a mongo query? We have an issue whereby since the clocks have gone forward (to BST) our mongo aggregation query does not group records as expected.
For example, say I have 3 records with a createdDate
of 2017-03-27 13:00:00
, 2017-03-28 00:30:00
, and 2017-03-28 13:00:00
I would expect the aggregation to group 1 record for 27/3 and 2 records for 28/3. However, because the second record is stored in the database in UTC as 2017-03-27 23:30:00
the aggregation groups 2 records for 27/3 and only 1 for 28/3.
This is the aggregation query:
{ "$match" : { "$and" : [
{ "createdDate" : { "$gte" : { "$date" : {ISODATE}} ,
"$lte" : { "$date" : {ISODATE}}}
]}},
{ "$group" : { "_id" : { "_id" : "$id" ,
"createdDate" : {
"year" : { "$year" : "$createdDate"} ,
"month" : { "$month" : "$createdDate"} ,
"day" : { "$dayOfMonth" : "$createdDate"}}} ,
"count" : { "$sum" : 1}
}}
Any ideas how to get around this issue?