Skip to content

Commit

Permalink
Merge pull request #492 from erans/master
Browse files Browse the repository at this point in the history
Fix: MongoDB: Date parsing and dates in aggregation $match
  • Loading branch information
arikfr committed Jul 15, 2015
2 parents 4ef3c27 + dbd3f75 commit 41b9b21
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions redash/query_runner/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import re
import time
from dateutil.parser import parse

from redash.utils import JSONEncoder
from redash.query_runner import *
Expand Down Expand Up @@ -151,10 +152,7 @@ def _get_column_by_name(self, columns, column_name):
def _convert_date(self, q, field_name):
m = date_regex.findall(q[field_name])
if len(m) > 0:
if q[field_name].find(":") == -1:
q[field_name] = datetime.datetime.fromtimestamp(time.mktime(time.strptime(m[0], "%Y-%m-%d")))
else:
q[field_name] = datetime.datetime.fromtimestamp(time.mktime(time.strptime(m[0], "%Y-%m-%d %H:%M")))
q[field_name] = parse(m[0], yearfirst=True)

def run_query(self, query):
if self.is_replica_set:
Expand Down Expand Up @@ -195,7 +193,16 @@ def run_query(self, query):
if "aggregate" in query_data:
aggregate = query_data["aggregate"]
for step in aggregate:
if "$sort" in step:
if "$match" in step:
for field in step["$match"]:
if type(step["$match"][field]) in [str, unicode]:
logging.debug(step["$match"][field])
self._convert_date(step["$match"], field)
elif type(step["$match"][field]) is dict:
for field2 in step["$match"][field]:
if type(step["$match"][field][field2]) in (str, unicode):
self._convert_date(step["$match"][field], field2)
elif "$sort" in step:
sort_list = []
for sort_item in step["$sort"]:
sort_list.append((sort_item["name"], sort_item["direction"]))
Expand Down

0 comments on commit 41b9b21

Please sign in to comment.