From f096c032cd12f2b50f777fde7d4d12dc2175e7e8 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Mon, 15 Aug 2011 01:09:25 +0400 Subject: [PATCH] Implement fetching of partial documents --- src/monger/collection.clj | 25 +++++++++++-------------- test/monger/test/collection.clj | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index d0c366c..c085887 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -20,19 +20,19 @@ (defn ^WriteResult insert ([^String collection, ^DBObject doc] - (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.insert coll (to-db-object doc) monger.core/*mongodb-write-concern*))) ([^String collection, ^DBObject doc, ^WriteConcern concern] - (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.insert coll (to-db-object doc) concern)))) (defn ^WriteResult insert-batch ([^String collection, ^List docs] - (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.insert coll (to-db-object docs) WriteConcern/NORMAL))) ([^String collection, ^List docs, ^WriteConcern concern] - (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.insert coll (to-db-object docs) concern)))) ;; monger.collection/find @@ -40,13 +40,13 @@ (defn ^DBCursor find ([^String collection] - (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.find coll))) ([^String collection, ^Map ref] - (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.find coll (to-db-object ref)))) ([^String collection, ^Map ref, ^List fields] - (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection) + (let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection) map-of-fields (fields-to-db-object fields)] (.find coll (to-db-object ref) (to-db-object map-of-fields)))) ) @@ -54,10 +54,10 @@ (defn ^DBObject find-by-id ([^String collection, ^String id] - (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.findOne coll (to-db-object { :_id id })))) ([^String collection, ^String id, ^List fields] - (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection) + (let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection) map-of-fields (fields-to-db-object fields)] (.findOne coll (to-db-object { :_id id }) (to-db-object map-of-fields)))) ) @@ -95,8 +95,5 @@ ;; (defn- fields-to-db-object - [fields] - (let [n (clojure.core/count fields) - ones (replicate n 1) - map-of-fields (zipmap fields ones)] - map-of-fields)) \ No newline at end of file + [^List fields] + (zipmap fields (repeat 1))) \ No newline at end of file diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index 915b672..b296ff0 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -141,3 +141,20 @@ (is (= 1 (.count (monger.collection/find collection { :language "Scala" })))) (is (= 3 (.count (monger.collection/find collection { :language "Clojure" })))) (is (empty? (monger.collection/find collection { :language "Java" }))))) + + +(deftest find-multiple-partial-documents + (let [collection "libraries"] + (monger.collection/remove collection) + (monger.collection/insert collection { :language "Clojure", :name "monger" }) + (monger.collection/insert collection { :language "Clojure", :name "langohr" }) + (monger.collection/insert collection { :language "Clojure", :name "incanter" }) + (monger.collection/insert collection { :language "Scala", :name "akka" }) + (let [scala-libs (monger.collection/find collection { :language "Scala" } [:name]) + clojure-libs (monger.collection/find collection { :language "Clojure"} [:language])] + (is (= 1 (.count scala-libs))) + (is (= 3 (.count clojure-libs))) + (doseq [i clojure-libs] + (let [doc (monger.convertion/from-db-object i true)] + (is (= (:language doc) "Clojure")))) + (is (empty? (monger.collection/find collection { :language "Erlang" } [:name]))))))