Add :keywordize option for aggregate that control if resulting map keys will be turned into keywords, default is true.

This commit is contained in:
Vladyslav Aleksakhin 2018-05-02 11:07:31 +02:00
parent 002b91d5e6
commit 7466db5c4d
2 changed files with 27 additions and 2 deletions

View file

@ -541,13 +541,17 @@
is supported, for specifying a limit on the execution time of the query in is supported, for specifying a limit on the execution time of the query in
milliseconds. 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." See http://docs.mongodb.org/manual/applications/aggregation/ to learn more."
[^DB db ^String coll stages & opts] [^DB db ^String coll stages & opts]
(let [coll (.getCollection db coll) (let [coll (.getCollection db coll)
agg-opts (build-aggregation-options opts) agg-opts (build-aggregation-options opts)
pipe (into-array-list (to-db-object stages)) pipe (into-array-list (to-db-object stages))
res (.aggregate coll pipe agg-opts)] res (.aggregate coll pipe agg-opts)
(map #(from-db-object % true) (iterator-seq res)))) {:keys [^Boolean keywordize]
:or {keywordize true}} opts]
(map #(from-db-object % keywordize) (iterator-seq res))))
(defn explain-aggregate (defn explain-aggregate
"Returns the explain plan for an aggregation query. MongoDB 2.2+ only. "Returns the explain plan for an aggregation query. MongoDB 2.2+ only.

View file

@ -15,6 +15,27 @@
(use-fixtures :each purge-collections) (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 (deftest test-basic-single-stage-$project-aggregation
(let [batch [{:state "CA" :quantity 1 :price 199.00} (let [batch [{:state "CA" :quantity 1 :price 199.00}
{:state "NY" :quantity 2 :price 199.00} {:state "NY" :quantity 2 :price 199.00}