Introduce monger.collection/find-one-as-map

This commit is contained in:
Michael S. Klishin 2011-09-02 04:08:31 +04:00
parent f8845111f2
commit 7e29ff457d
2 changed files with 43 additions and 7 deletions

View file

@ -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))
(:import (com.mongodb Mongo DB DBCollection WriteResult DBObject WriteConcern DBCursor) (java.util List Map) (clojure.lang IPersistentMap))
(:require [monger core result])
(:use [monger.convertion]))
@ -68,6 +68,15 @@
map-of-fields (fields-to-db-object fields)]
(.findOne #^DBCollection coll #^DBObject (to-db-object ref) #^DBObject (to-db-object map-of-fields)))))
(defn ^IPersistentMap find-one-as-map
([^String collection, ^Map ref]
(from-db-object ^DBObject (find-one collection ref) true))
([^String collection, ^Map ref, keywordize]
(from-db-object ^DBObject (find-one collection ref) keywordize))
([^String collection, ^Map ref, ^List fields, keywordize]
(from-db-object ^DBObject (find-one collection ref fields) keywordize)))
;;
;; monger.collection/find-by-id

View file

@ -33,9 +33,9 @@
(let [collection "people"
doc (monger.convertion/to-db-object { :name "Joe", :age 30 })]
(monger.collection/remove collection)
(is (nil? (.get doc "_id")))
(is (nil? (.get ^DBObject doc "_id")))
(monger.collection/insert "people" doc)
(is (not (nil? (.get doc "_id"))))))
(is (not (nil? (.get ^DBObject doc "_id"))))))
@ -123,6 +123,12 @@
(monger.collection/remove collection)
(is (nil? (monger.collection/find-one collection {})))))
(deftest find-one-full-document-as-map-when-collection-is-empty
(let [collection "docs"]
(monger.collection/remove collection)
(is (nil? (monger.collection/find-one-as-map collection {})))))
(deftest find-one-full-document-when-collection-has-matches
(let [collection "docs"
doc-id (monger.util/random-uuid)
@ -130,10 +136,21 @@
(monger.collection/remove collection)
(monger.collection/insert collection doc)
(def #^DBObject found-one (monger.collection/find-one collection { :language "Clojure" }))
(is (= (:_id doc) (.get found-one "_id")))
(is (= (:_id doc) (.get ^DBObject found-one "_id")))
(is (= (monger.convertion/from-db-object found-one true) doc))
(is (= (monger.convertion/to-db-object doc) found-one))))
(deftest find-one-full-document-as-map-when-collection-has-matches
(let [collection "docs"
doc-id (monger.util/random-uuid)
doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }]
(monger.collection/remove collection)
(monger.collection/insert collection doc)
(is (= doc (monger.collection/find-one-as-map collection { :language "Clojure" })))))
(deftest find-one-partial-document-when-collection-has-matches
(let [collection "docs"
doc-id (monger.util/random-uuid)
@ -142,11 +159,21 @@
(monger.collection/remove collection)
(monger.collection/insert collection doc)
(def #^DBObject loaded (monger.collection/find-one collection { :language "Clojure" } fields))
(is (nil? (.get #^DBObject loaded "data-stire")))
(is (nil? (.get #^DBObject loaded "data-store")))
(is (= doc-id (.get #^DBObject loaded "_id")))
(is (= "Clojure" (.get #^DBObject loaded "language")))))
(deftest find-one-partial-document-as-map-when-collection-has-matches
(let [collection "docs"
doc-id (monger.util/random-uuid)
doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }
fields [:data-store]]
(monger.collection/remove collection)
(monger.collection/insert collection doc)
(is (= { :data-store "MongoDB", :_id doc-id } (monger.collection/find-one-as-map collection { :language "Clojure" } fields true)))))
;;
;; find-by-id
@ -256,9 +283,9 @@
(let [collection "people"
doc (monger.convertion/to-db-object { :name "Joe", :age 30 })]
(monger.collection/remove collection)
(is (nil? (.get doc "_id")))
(is (nil? (.get ^DBObject doc "_id")))
(monger.collection/save "people" doc)
(is (not (nil? (.get doc "_id"))))))
(is (not (nil? (.get ^DBObject doc "_id"))))))