This commit is contained in:
Oleksandr Petrov 2011-12-03 18:40:52 +01:00
commit e6f81e52a7
4 changed files with 26 additions and 9 deletions

View file

@ -4,7 +4,7 @@
:dependencies [[org.clojure/clojure "1.3.0"] :dependencies [[org.clojure/clojure "1.3.0"]
[org.mongodb/mongo-java-driver "2.7.2"] [org.mongodb/mongo-java-driver "2.7.2"]
[com.novemberain/validateur "1.0.0-SNAPSHOT"]] [com.novemberain/validateur "1.0.0-SNAPSHOT"]]
:dev-dependencies [[org.clojure/data.json "0.1.2"] :dev-dependencies [[org.clojure/data.json "0.1.2" :exclusions [org.clojure/clojure]]
[clj-time "0.3.2-SNAPSHOT" :exclusions [org.clojure/clojure]]] [clj-time "0.3.3" :exclusions [org.clojure/clojure]]]
:dev-resources-path "test/resources" :dev-resources-path "test/resources"
:warn-on-reflection true) :warn-on-reflection true)

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,13 +150,19 @@
(monger.collection/count collection) (monger.collection/count collection)
(monger.collection/count collection { :first_name \"Paul\" })" (monger.collection/count collection { :first_name \"Paul\" })"
([^String collection] (^long [^String collection]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.count coll))) (.count coll)))
([^String collection, ^Map conditions] (^long [^String collection, ^Map conditions]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.count coll (to-db-object conditions))))) (.count coll (to-db-object conditions)))))
(defn any?
([^String collection]
(> (count collection) 0))
([^String collection, ^Map conditions]
(> (count collection conditions) 0)))
;; monger.collection/update ;; monger.collection/update

View file

@ -41,4 +41,4 @@
IPersistentMap IPersistentMap
(get-id (get-id
[^IPersistentMap object] [^IPersistentMap object]
(or (:_id object) ("_id" object)))) (or (:_id object) (object "_id"))))

View file

@ -46,6 +46,13 @@
(mgcol/insert "people" doc) (mgcol/insert "people" doc)
(is (not (nil? (monger.util/get-id doc)))))) (is (not (nil? (monger.util/get-id doc))))))
(deftest insert-a-map-with-id-and-with-default-write-concern
(let [collection "people"
id (ObjectId.)
doc { :name "Joe", :age 30 "_id" id }
result (mgcol/insert "people" doc)]
(is (= id (monger.util/get-id doc)))))
;; ;;
@ -79,9 +86,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