         $fields              (map field-id->$string field-ids)
         fields->$fields      (zipmap fields $fields)]
     (aggregate {$group  (merge {"_id"    (if (= (count fields) 1) (first $fields)
-                                             fields->$fields)
-                                ag-field ag-clause}
+                                             fields->$fields)}
+                               (when (and ag-field ag-clause)
+                                 {ag-field ag-clause})
                                (->> fields->$fields
                                     (map (fn [[field $field]]
                                            (when-not (= field "_id")

  ;; or are we missing something?
  ;; At any rate these seem to be the most common use cases, so we can add more here if and when they're needed.
  (match aggregation
    ["count"]        ["count" {$sum 1}]
    ["avg" field-id] ["avg" {$avg (field-id->$string field-id)}]
    ["sum" field-id] ["sum" {$sum (field-id->$string field-id)}]))

                                                                          "ascending" 1
                                                                          "descending" -1)]))
                              (apply sorted-map))}
               {$project (merge {"_id"    false}
                                (when ag-field
                                  {ag-field true})
                                (zipmap fields (repeat true)))}
               (when limit
                 {$limit limit}))))
