Introduce monger.collection/find-map-by-id
This commit is contained in:
parent
6d17366bdd
commit
a670b31462
2 changed files with 40 additions and 3 deletions
|
|
@ -91,6 +91,14 @@
|
||||||
map-of-fields (fields-to-db-object fields)]
|
map-of-fields (fields-to-db-object fields)]
|
||||||
(.findOne #^DBCollection coll #^DBObject (to-db-object { :_id id }) #^DBObject (to-db-object map-of-fields)))))
|
(.findOne #^DBCollection coll #^DBObject (to-db-object { :_id id }) #^DBObject (to-db-object map-of-fields)))))
|
||||||
|
|
||||||
|
(defn ^IPersistentMap find-map-by-id
|
||||||
|
([^String collection, ^String id]
|
||||||
|
(from-db-object ^DBObject (find-by-id collection id) true))
|
||||||
|
([^String collection, ^String id, keywordize]
|
||||||
|
(from-db-object ^DBObject (find-by-id collection id) keywordize))
|
||||||
|
([^String collection, ^String id, ^List fields, keywordize]
|
||||||
|
(from-db-object ^DBObject (find-by-id collection id fields) keywordize)))
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; monger.collection/group
|
;; monger.collection/group
|
||||||
|
|
|
||||||
|
|
@ -179,14 +179,21 @@
|
||||||
;; find-by-id
|
;; find-by-id
|
||||||
;;
|
;;
|
||||||
|
|
||||||
(deftest find-full-document-by-id-when-document-does-not-exist
|
(deftest find-full-document-by-id-when-that-document-does-not-exist
|
||||||
(let [collection "libraries"
|
(let [collection "libraries"
|
||||||
doc-id (monger.util/random-uuid)]
|
doc-id (monger.util/random-uuid)]
|
||||||
(monger.collection/remove collection)
|
(monger.collection/remove collection)
|
||||||
(is (nil? (monger.collection/find-by-id collection doc-id)))))
|
(is (nil? (monger.collection/find-by-id collection doc-id)))))
|
||||||
|
|
||||||
|
|
||||||
(deftest find-full-document-by-id-when-document-exists
|
(deftest find-full-document-by-id-as-map-when-that-document-does-not-exist
|
||||||
|
(let [collection "libraries"
|
||||||
|
doc-id (monger.util/random-uuid)]
|
||||||
|
(monger.collection/remove collection)
|
||||||
|
(is (nil? (monger.collection/find-map-by-id collection doc-id)))))
|
||||||
|
|
||||||
|
|
||||||
|
(deftest find-full-document-by-id-when-document-does-exist
|
||||||
(let [collection "libraries"
|
(let [collection "libraries"
|
||||||
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 }]
|
||||||
|
|
@ -194,7 +201,16 @@
|
||||||
(monger.collection/insert collection doc)
|
(monger.collection/insert collection doc)
|
||||||
(is (= (doc (monger.collection/find-by-id collection doc-id))))))
|
(is (= (doc (monger.collection/find-by-id collection doc-id))))))
|
||||||
|
|
||||||
(deftest find-partial-document-by-id-when-document-exists
|
(deftest find-full-document-map-by-id-when-document-does-exist
|
||||||
|
(let [collection "libraries"
|
||||||
|
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-map-by-id collection doc-id))))))
|
||||||
|
|
||||||
|
|
||||||
|
(deftest find-partial-document-by-id-when-document-does-exist
|
||||||
(let [collection "libraries"
|
(let [collection "libraries"
|
||||||
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 }]
|
||||||
|
|
@ -203,6 +219,19 @@
|
||||||
(is (= ({ :language "Clojure" } (monger.collection/find-by-id collection doc-id [ :language ]))))))
|
(is (= ({ :language "Clojure" } (monger.collection/find-by-id collection doc-id [ :language ]))))))
|
||||||
|
|
||||||
|
|
||||||
|
(deftest find-partial-document-as-map-by-id-when-document-does-exist
|
||||||
|
(let [collection "libraries"
|
||||||
|
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 (= ({ :language "Clojure" } (monger.collection/find-map-by-id collection doc-id [ :language ]))))))
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; monger.collection/find
|
||||||
|
;;
|
||||||
|
|
||||||
(deftest find-multiple-documents-when-collection-is-empty
|
(deftest find-multiple-documents-when-collection-is-empty
|
||||||
(let [collection "libraries"]
|
(let [collection "libraries"]
|
||||||
(monger.collection/remove collection)
|
(monger.collection/remove collection)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue