From be063978495018b5b93c8374790ece5793c7a374 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Mon, 28 Nov 2011 20:12:19 +0400 Subject: [PATCH] Introduce monger.collection/any? Useful for detecting duplicate documents and so on --- src/monger/collection.clj | 16 +++++++++++----- test/monger/test/collection.clj | 6 +++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index f28d92a..95b1226 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -142,7 +142,7 @@ ;; ;; monger.collection/count ;; -(defn ^long count +(defn count "Returns the number of documents in this collection. Takes optional conditions as an argument. @@ -150,12 +150,18 @@ (monger.collection/count collection) (monger.collection/count collection { :first_name \"Paul\" })" + (^long [^String collection] + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.count coll))) + (^long [^String collection, ^Map conditions] + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.count coll (to-db-object conditions))))) + +(defn any? ([^String collection] - (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] - (.count coll))) + (> (count collection) 0)) ([^String collection, ^Map conditions] - (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] - (.count coll (to-db-object conditions))))) + (> (count collection conditions) 0))) ;; monger.collection/update diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index b1c5ed7..c1b3a34 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -79,9 +79,13 @@ { :language "Clojure", :name "incanter" } { :language "Scala", :name "akka" }] ) (is (= 4 (mgcol/count collection))) + (is (mgcol/any? collection)) (is (= 3 (mgcol/count collection { :language "Clojure" }))) + (is (mgcol/any? collection { :language "Clojure" })) (is (= 1 (mgcol/count collection { :language "Scala" }))) - (is (= 0 (mgcol/count collection { :language "Python" }))))) + (is (mgcol/any? collection { :language "Scala" })) + (is (= 0 (mgcol/count collection { :language "Python" }))) + (is (not (mgcol/any? collection { :language "Python" }))))) (deftest remove-all-documents-from-collection