diff --git a/cubes-mongo/browser.py b/cubes-mongo/browser.py index 202316b..42d4ebd 100644 --- a/cubes-mongo/browser.py +++ b/cubes-mongo/browser.py @@ -403,7 +403,10 @@ def _date_transform(item, date_field): group = phys.group elif function: group_applicator = function["group_by"] - group = group_applicator(escape_level(agg.measure.ref())) + if agg.measure: + group = group_applicator(escape_level(agg.measure)) + else: + group = group_applicator(escape_level(agg.ref())) else: raise ModelError("Neither function or mapping group specified " "for aggregate '%s' in cube '%s'" @@ -420,10 +423,11 @@ def _date_transform(item, date_field): pipeline = [] pipeline.append({ "$match": query_obj }) - if fields_obj: - pipeline.append({ "$project": fields_obj }) pipeline.append({ "$group": group_obj }) + if fields_obj: + pipeline.append({ "$project": fields_obj }) + if not timezone_shift_processing: if order: obj = { diff --git a/cubes-mongo/functions.py b/cubes-mongo/functions.py index bcf6a28..2179d46 100644 --- a/cubes-mongo/functions.py +++ b/cubes-mongo/functions.py @@ -32,7 +32,19 @@ 'custom': { 'group_by' : (lambda field: { '$sum': 1 }), 'aggregate_fn': len - } + }, + 'min': { + 'group_by': (lambda field: { '$min': "$%s" % field }), + 'aggregate_fn': min, + }, + 'max': { + 'group_by': (lambda field: { '$max': "$%s" % field }), + 'aggregate_fn': max, + }, + 'avg': { + 'group_by': (lambda field: { '$avg': "$%s" % field }), + 'aggregate_fn': sum, + }, }