Make monger.collection/find-one return a DBObject, not a cursor

This commit is contained in:
Michael S. Klishin 2011-09-02 03:28:40 +04:00
parent adfa422a51
commit f8845111f2
2 changed files with 9 additions and 11 deletions

View file

@ -59,14 +59,14 @@
;; monger.collection/find-one ;; monger.collection/find-one
;; ;;
(defn ^DBCursor find-one (defn ^DBObject find-one
([^String collection, ^Map ref] ([^String collection, ^Map ref]
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.find #^DBCollection coll #^DBObject (to-db-object ref)))) (.findOne #^DBCollection coll #^DBObject (to-db-object ref))))
([^String collection, ^Map ref, ^List fields] ([^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)] map-of-fields (fields-to-db-object fields)]
(.find #^DBCollection coll #^DBObject (to-db-object ref) #^DBObject (to-db-object map-of-fields))))) (.findOne #^DBCollection coll #^DBObject (to-db-object ref) #^DBObject (to-db-object map-of-fields)))))
;; ;;

View file

@ -121,9 +121,7 @@
(deftest find-one-full-document-when-collection-is-empty (deftest find-one-full-document-when-collection-is-empty
(let [collection "docs"] (let [collection "docs"]
(monger.collection/remove collection) (monger.collection/remove collection)
(def cursor (monger.collection/find-one collection {})) (is (nil? (monger.collection/find-one collection {})))))
(is (instance? DBCursor cursor))
(is (empty? cursor))))
(deftest find-one-full-document-when-collection-has-matches (deftest find-one-full-document-when-collection-has-matches
(let [collection "docs" (let [collection "docs"
@ -131,19 +129,19 @@
doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }] doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }]
(monger.collection/remove collection) (monger.collection/remove collection)
(monger.collection/insert collection doc) (monger.collection/insert collection doc)
(def cursor (monger.collection/find-one collection { :language "Clojure" })) (def #^DBObject found-one (monger.collection/find-one collection { :language "Clojure" }))
(is (= (:_id doc) (.get (.next #^DBCursor cursor) "_id"))))) (is (= (:_id doc) (.get found-one "_id")))
(is (= (monger.convertion/to-db-object doc) found-one))))
(deftest find-one-full-document-when-collection-has-matches (deftest find-one-partial-document-when-collection-has-matches
(let [collection "docs" (let [collection "docs"
doc-id (monger.util/random-uuid) doc-id (monger.util/random-uuid)
doc { :data-store "MongoDB", :language "Clojure", :_id doc-id } doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }
fields [:language]] fields [:language]]
(monger.collection/remove collection) (monger.collection/remove collection)
(monger.collection/insert collection doc) (monger.collection/insert collection doc)
(def cursor (monger.collection/find-one collection { :language "Clojure" } fields)) (def #^DBObject loaded (monger.collection/find-one collection { :language "Clojure" } fields))
(def #^DBObject loaded (.next #^DBCursor cursor))
(is (nil? (.get #^DBObject loaded "data-stire"))) (is (nil? (.get #^DBObject loaded "data-stire")))
(is (= doc-id (.get #^DBObject loaded "_id"))) (is (= doc-id (.get #^DBObject loaded "_id")))
(is (= "Clojure" (.get #^DBObject loaded "language"))))) (is (= "Clojure" (.get #^DBObject loaded "language")))))