Introduce monger.collection/find-maps

This commit is contained in:
Michael S. Klishin 2011-09-03 18:31:26 +04:00
parent 1609f0fab2
commit 4700213bd5
2 changed files with 23 additions and 1 deletions

View file

@ -8,7 +8,7 @@
;; You must not remove this notice, or any other, from this software. ;; You must not remove this notice, or any other, from this software.
(ns monger.collection (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]) (:require [monger core result])
(:use [monger.convertion])) (:use [monger.convertion]))
@ -54,6 +54,13 @@
map-of-fields (fields-to-db-object fields)] map-of-fields (fields-to-db-object fields)]
(.find #^DBCollection coll #^DBObject (to-db-object ref) #^DBObject (to-db-object map-of-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 ;; monger.collection/find-one

View file

@ -246,6 +246,10 @@
(let [collection "libraries"] (let [collection "libraries"]
(is (empty? (monger.collection/find collection { :language "Scala" }))))) (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 (deftest find-multiple-documents
(let [collection "libraries"] (let [collection "libraries"]
@ -257,6 +261,17 @@
(is (= 3 (.count (monger.collection/find collection { :language "Clojure" })))) (is (= 3 (.count (monger.collection/find collection { :language "Clojure" }))))
(is (empty? (monger.collection/find collection { :language "Java" }))))) (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 (deftest find-multiple-partial-documents
(let [collection "libraries"] (let [collection "libraries"]