From 7466db5c4d8aa6b9bc40ed3c59506d846eb06a9b Mon Sep 17 00:00:00 2001 From: Vladyslav Aleksakhin Date: Wed, 2 May 2018 11:07:31 +0200 Subject: [PATCH] Add :keywordize option for aggregate that control if resulting map keys will be turned into keywords, default is true. --- src/clojure/monger/collection.clj | 8 +++++-- .../test/aggregation_framework_test.clj | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/clojure/monger/collection.clj b/src/clojure/monger/collection.clj index 2c3addd..0a50e97 100644 --- a/src/clojure/monger/collection.clj +++ b/src/clojure/monger/collection.clj @@ -541,13 +541,17 @@ is supported, for specifying a limit on the execution time of the query in milliseconds. + :keywordize option that control if resulting map keys will be turned into keywords, default is true. + See http://docs.mongodb.org/manual/applications/aggregation/ to learn more." [^DB db ^String coll stages & opts] (let [coll (.getCollection db coll) agg-opts (build-aggregation-options opts) pipe (into-array-list (to-db-object stages)) - res (.aggregate coll pipe agg-opts)] - (map #(from-db-object % true) (iterator-seq res)))) + res (.aggregate coll pipe agg-opts) + {:keys [^Boolean keywordize] + :or {keywordize true}} opts] + (map #(from-db-object % keywordize) (iterator-seq res)))) (defn explain-aggregate "Returns the explain plan for an aggregation query. MongoDB 2.2+ only. diff --git a/test/monger/test/aggregation_framework_test.clj b/test/monger/test/aggregation_framework_test.clj index b243168..76abf9c 100644 --- a/test/monger/test/aggregation_framework_test.clj +++ b/test/monger/test/aggregation_framework_test.clj @@ -15,6 +15,27 @@ (use-fixtures :each purge-collections) + (deftest test-basic-single-stage-$project-aggregation-no-keywordize + (let [batch [{"state" "CA" "quantity" 1 "price" 199.00} + {"state" "NY" "quantity" 2 "price" 199.00} + {"state" "NY" "quantity" 1 "price" 299.00} + {"state" "IL" "quantity" 2 "price" 11.50 } + {"state" "CA" "quantity" 2 "price" 2.95 } + {"state" "IL" "quantity" 3 "price" 5.50 }] + expected #{{"quantity" 1 "state" "CA"} + {"quantity" 2 "state" "NY"} + {"quantity" 1 "state" "NY"} + {"quantity" 2 "state" "IL"} + {"quantity" 2 "state" "CA"} + {"quantity" 3 "state" "IL"}}] + (mc/insert-batch db coll batch) + (is (= 6 (mc/count db coll))) + (let [result (->> + (mc/aggregate db coll [{$project {"state" 1 "quantity" 1}}] :keywordize false) + (map #(select-keys % ["state" "quantity"])) + (set))] + (is (= expected result))))) + (deftest test-basic-single-stage-$project-aggregation (let [batch [{:state "CA" :quantity 1 :price 199.00} {:state "NY" :quantity 2 :price 199.00}