From be063978495018b5b93c8374790ece5793c7a374 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Mon, 28 Nov 2011 20:12:19 +0400 Subject: [PATCH 1/4] 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 From 4e66df532609991663e743ff1ff3cd7417947bcd Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Mon, 28 Nov 2011 20:29:38 +0400 Subject: [PATCH 2/4] cljt-time 0.3.3 is out --- project.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project.clj b/project.clj index 713ec5c..6806ab4 100644 --- a/project.clj +++ b/project.clj @@ -5,6 +5,6 @@ [org.mongodb/mongo-java-driver "2.7.2"] [com.novemberain/validateur "1.0.0-SNAPSHOT"]] :dev-dependencies [[org.clojure/data.json "0.1.2"] - [clj-time "0.3.2-SNAPSHOT" :exclusions [org.clojure/clojure]]] + [clj-time "0.3.3" :exclusions [org.clojure/clojure]]] :dev-resources-path "test/resources" :warn-on-reflection true) From c8fe274329d47c1181da1d1a1e3dfe665143cc3e Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Mon, 28 Nov 2011 21:05:32 +0400 Subject: [PATCH 3/4] Exclude Clojure dependency for clojure.data.json --- project.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project.clj b/project.clj index 6806ab4..bdf9747 100644 --- a/project.clj +++ b/project.clj @@ -4,7 +4,7 @@ :dependencies [[org.clojure/clojure "1.3.0"] [org.mongodb/mongo-java-driver "2.7.2"] [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.3" :exclusions [org.clojure/clojure]]] :dev-resources-path "test/resources" :warn-on-reflection true) From 8127d26442faf9ed4f3ce436f73bd59c427bd19a Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Tue, 29 Nov 2011 15:44:55 +0400 Subject: [PATCH 4/4] Fix monger.util/get-id for persistent maps with string keys --- src/monger/util.clj | 2 +- test/monger/test/collection.clj | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/monger/util.clj b/src/monger/util.clj index 8651ab3..78707d7 100644 --- a/src/monger/util.clj +++ b/src/monger/util.clj @@ -41,4 +41,4 @@ IPersistentMap (get-id [^IPersistentMap object] - (or (:_id object) ("_id" object)))) + (or (:_id object) (object "_id")))) diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index c1b3a34..9e5012f 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -46,6 +46,13 @@ (mgcol/insert "people" 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))))) + ;;