                     (convert-keys name))
                 {})
         agg-fields (->> aggregations
-                        (map (comp second second))
-                        (remove nil?))
+                        (map (juxt first (comp second second)))
+                        (remove #(nil? (second %)))
+                        (into {}))
 
-        map-fn (generate-map-fn group agg-fields field-zip-fn)
+        map-fn (generate-map-fn
+                (zipmap group group)
+                agg-fields
+                field-zip-fn)        
         reduce-fn (generate-reduce-fn aggregations)]
     (array-map :mapReduce (name from)
                :map map-fn


(defn- generate-reduce-fn
  [aggregations]
  (let [reduce-obj (reduce (fn [acc [to-field [agg from-field]]]
                             (merge acc
                                    {(name to-field)
                                     (js* ((aget aggs (clj agg)) values (clj from-field)))}))
                           {} aggregations)]
    (js* (fn [key values]
           (var aggs (clj agg-fns))

               (return (.apply (aget Math "max") nil vals))))
   :avg (js* (fn [ary field]
               (var vals (.map ary (fn [obj] (return (aget obj field)))))
               (return (.avg Array vals))))
   :median (js* (fn [ary field]
                  (var median (fn [array]
                                (var array (.sort array))
