Enable query plan explanation for the aggregation framework
Add the explain-aggregate function, which returns a map containing information about the given aggregation query.
This commit is contained in:
parent
136dea00b2
commit
f0946acd75
2 changed files with 20 additions and 1 deletions
|
|
@ -515,7 +515,16 @@
|
||||||
res (.aggregate coll pipe agg-opts)]
|
res (.aggregate coll pipe agg-opts)]
|
||||||
(map #(from-db-object % true) (iterator-seq res))))
|
(map #(from-db-object % true) (iterator-seq res))))
|
||||||
|
|
||||||
|
(defn explain-aggregate
|
||||||
|
"Returns the explain plan for an aggregation query. MongoDB 2.2+ only.
|
||||||
|
|
||||||
|
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 (java.util.ArrayList. (to-db-object stages))
|
||||||
|
res (.explainAggregate coll pipe agg-opts)]
|
||||||
|
(from-db-object res true)))
|
||||||
;;
|
;;
|
||||||
;; Misc
|
;; Misc
|
||||||
;;
|
;;
|
||||||
|
|
|
||||||
|
|
@ -104,4 +104,14 @@
|
||||||
(is (= 6 (mc/count db coll)))
|
(is (= 6 (mc/count db coll)))
|
||||||
(let [result (set (map #(select-keys % [:state :quantity])
|
(let [result (set (map #(select-keys % [:state :quantity])
|
||||||
(mc/aggregate db coll [{$project {:state 1 :quantity 1}}] :cursor {:batch-size 10})))]
|
(mc/aggregate db coll [{$project {:state 1 :quantity 1}}] :cursor {:batch-size 10})))]
|
||||||
(is (= expected result))))))
|
(is (= expected result)))))
|
||||||
|
|
||||||
|
(deftest test-explain-aggregate
|
||||||
|
(let [batch [{:state "CA" :price 100}
|
||||||
|
{:state "CA" :price 10}
|
||||||
|
{:state "IL" :price 50}]
|
||||||
|
expected-keys #{:ok :stages}]
|
||||||
|
(mc/insert-batch db coll batch)
|
||||||
|
(let [result (mc/explain-aggregate db coll [{$match {:state "CA"}}])
|
||||||
|
key-in-result? (partial contains? result)]
|
||||||
|
(is (every? key-in-result? expected-keys))))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue