      (generate-map-fn group-fields agg-fields identity))
   ([group-fields agg-fields zip-fn]
      (let [create-map-obj (fn [fields]
-                            (reduce (fn [acc field]
-                                      (let [from-field (name (zip-fn field))]
-                                        (merge acc {field (js* (aget this (clj from-field)))})))
+                            (reduce (fn [acc [out-field in-field]]
+                                      (let [from-field (name (zip-fn in-field))]
+                                        (merge acc {out-field (js* (aget this (clj from-field)))})))
                                     {} fields))
            map-id (create-map-obj group-fields)
            map-val (create-map-obj agg-fields)]

                    (convert-keys name))
                {})
        agg-fields (->> aggregations
                        (map (comp second second))
                        (remove nil?))

        map-fn (generate-map-fn 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 [out-field [agg in-field]]]
                             (merge acc
                                    {(name out-field)
                                     (js* ((aget aggs (clj agg)) values (clj (name out-field))))}))
                           {} aggregations)]
    (js* (fn [key values]
           (var aggs (clj agg-fns))
