Introduce monger.collection/any?

Useful for detecting duplicate documents and so on
This commit is contained in:
Michael S. Klishin 2011-11-28 20:12:19 +04:00
parent 6c4e88842c
commit be06397849
2 changed files with 16 additions and 6 deletions

View file

@ -142,7 +142,7 @@
;; ;;
;; monger.collection/count ;; monger.collection/count
;; ;;
(defn ^long count (defn count
"Returns the number of documents in this collection. "Returns the number of documents in this collection.
Takes optional conditions as an argument. Takes optional conditions as an argument.
@ -150,12 +150,18 @@
(monger.collection/count collection) (monger.collection/count collection)
(monger.collection/count collection { :first_name \"Paul\" })" (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] ([^String collection]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (> (count collection) 0))
(.count coll)))
([^String collection, ^Map conditions] ([^String collection, ^Map conditions]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (> (count collection conditions) 0)))
(.count coll (to-db-object conditions)))))
;; monger.collection/update ;; monger.collection/update

View file

@ -79,9 +79,13 @@
{ :language "Clojure", :name "incanter" } { :language "Clojure", :name "incanter" }
{ :language "Scala", :name "akka" }] ) { :language "Scala", :name "akka" }] )
(is (= 4 (mgcol/count collection))) (is (= 4 (mgcol/count collection)))
(is (mgcol/any? collection))
(is (= 3 (mgcol/count collection { :language "Clojure" }))) (is (= 3 (mgcol/count collection { :language "Clojure" })))
(is (mgcol/any? collection { :language "Clojure" }))
(is (= 1 (mgcol/count collection { :language "Scala" }))) (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 (deftest remove-all-documents-from-collection