From 4700213bd5814bdd425d6cf64167601466ea6cad Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sat, 3 Sep 2011 18:31:26 +0400 Subject: [PATCH] Introduce monger.collection/find-maps --- src/monger/collection.clj | 9 ++++++++- test/monger/test/collection.clj | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index f0aa813..4c86001 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -8,7 +8,7 @@ ;; You must not remove this notice, or any other, from this software. (ns monger.collection - (:import (com.mongodb Mongo DB DBCollection WriteResult DBObject WriteConcern DBCursor) (java.util List Map) (clojure.lang IPersistentMap)) + (:import (com.mongodb Mongo DB DBCollection WriteResult DBObject WriteConcern DBCursor) (java.util List Map) (clojure.lang IPersistentMap ISeq)) (:require [monger core result]) (:use [monger.convertion])) @@ -54,6 +54,13 @@ map-of-fields (fields-to-db-object fields)] (.find #^DBCollection coll #^DBObject (to-db-object ref) #^DBObject (to-db-object map-of-fields))))) +(defn ^ISeq find-maps + ([^String collection] + (map (fn [x] (from-db-object x true)) (seq (find collection)))) + ([^String collection, ^Map ref] + (map (fn [x] (from-db-object x true)) (seq (find collection ref)))) + ([^String collection, ^Map ref, ^List fields] + (map (fn [x] (from-db-object x true)) (seq (find collection ref fields))))) ;; ;; monger.collection/find-one diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index b2e00cc..c48de95 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -246,6 +246,10 @@ (let [collection "libraries"] (is (empty? (monger.collection/find collection { :language "Scala" }))))) +(deftest find-multiple-maps-when-collection-is-empty + (let [collection "libraries"] + (is (empty? (monger.collection/find-maps collection { :language "Scala" }))))) + (deftest find-multiple-documents (let [collection "libraries"] @@ -257,6 +261,17 @@ (is (= 3 (.count (monger.collection/find collection { :language "Clojure" })))) (is (empty? (monger.collection/find collection { :language "Java" }))))) +(deftest find-multiple-maps + (let [collection "libraries"] + (monger.collection/insert-batch collection [{ :language "Clojure", :name "monger" } + { :language "Clojure", :name "langohr" }, + { :language "Clojure", :name "incanter" }, + { :language "Scala", :name "akka" }]) + (is (= 1 (clojure.core/count (monger.collection/find-maps collection { :language "Scala" })))) + (is (= 3 (.count (monger.collection/find-maps collection { :language "Clojure" })))) + (is (empty? (monger.collection/find-maps collection { :language "Java" }))))) + + (deftest find-multiple-partial-documents (let [collection "libraries"]