From c81026e308f4c98f4176f5cb672e0ac2a11db357 Mon Sep 17 00:00:00 2001 From: Matti Jagula Date: Wed, 4 Dec 2013 21:43:40 +0200 Subject: [PATCH] Added support for $first and $last aggregation operators. --- src/clojure/monger/operators.clj | 3 +++ .../test/aggregation_framework_test.clj | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/clojure/monger/operators.clj b/src/clojure/monger/operators.clj index 5cdd24b..8fd13ec 100644 --- a/src/clojure/monger/operators.clj +++ b/src/clojure/monger/operators.clj @@ -210,6 +210,9 @@ (defoperator $avg) (defoperator $sum) +(defoperator $first) +(defoperator $last) + (defoperator $add) (defoperator $divide) (defoperator $multiply) diff --git a/test/monger/test/aggregation_framework_test.clj b/test/monger/test/aggregation_framework_test.clj index 7af2f2f..e3d2aba 100644 --- a/test/monger/test/aggregation_framework_test.clj +++ b/test/monger/test/aggregation_framework_test.clj @@ -67,3 +67,22 @@ {$group {:_id "$state" :total {$sum "$subtotal"}}}]))] (is (= expected result))))) + + +(deftest test-$first-aggregation-operator + (let [collection "docs" + batch [{ :state "CA" } + { :state "IL"}] + expected "CA"] + (mc/insert-batch collection batch) + (let [result (:state (first (mc/aggregate collection [{$group { :_id 1 :state {$first "$state"} }}])))] + (is (= expected result))))) + +(deftest test-$last-aggregation-operator + (let [collection "docs" + batch [{ :state "CA" } + { :state "IL"}] + expected "IL"] + (mc/insert-batch collection batch) + (let [result (:state (first (mc/aggregate collection [{$group { :_id 1 :state {$last "$state"} }}])))] + (is (= expected result)))))