From f8845111f27a2bce76207e5c0725e9e01c150708 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Fri, 2 Sep 2011 03:28:40 +0400 Subject: [PATCH] Make monger.collection/find-one return a DBObject, not a cursor --- src/monger/collection.clj | 6 +++--- test/monger/test/collection.clj | 14 ++++++-------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 3d6d44f..2143b0b 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -59,14 +59,14 @@ ;; monger.collection/find-one ;; -(defn ^DBCursor find-one +(defn ^DBObject find-one ([^String collection, ^Map ref] (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] (let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection) 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))))) ;; diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index 36355d9..1bc8e7b 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -121,9 +121,7 @@ (deftest find-one-full-document-when-collection-is-empty (let [collection "docs"] (monger.collection/remove collection) - (def cursor (monger.collection/find-one collection {})) - (is (instance? DBCursor cursor)) - (is (empty? cursor)))) + (is (nil? (monger.collection/find-one collection {}))))) (deftest find-one-full-document-when-collection-has-matches (let [collection "docs" @@ -131,19 +129,19 @@ doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }] (monger.collection/remove collection) (monger.collection/insert collection doc) - (def cursor (monger.collection/find-one collection { :language "Clojure" })) - (is (= (:_id doc) (.get (.next #^DBCursor cursor) "_id"))))) + (def #^DBObject found-one (monger.collection/find-one collection { :language "Clojure" })) + (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" doc-id (monger.util/random-uuid) doc { :data-store "MongoDB", :language "Clojure", :_id doc-id } fields [:language]] (monger.collection/remove collection) (monger.collection/insert collection doc) - (def cursor (monger.collection/find-one collection { :language "Clojure" } fields)) - (def #^DBObject loaded (.next #^DBCursor cursor)) + (def #^DBObject loaded (monger.collection/find-one collection { :language "Clojure" } fields)) (is (nil? (.get #^DBObject loaded "data-stire"))) (is (= doc-id (.get #^DBObject loaded "_id"))) (is (= "Clojure" (.get #^DBObject loaded "language")))))