Extract function

This commit is contained in:
Michael S. Klishin 2011-08-15 00:25:12 +04:00
parent f8686896f5
commit 1c71375b16

View file

@ -36,6 +36,7 @@
(.insert coll (to-db-object docs) concern)))) (.insert coll (to-db-object docs) concern))))
;; monger.collection/find ;; monger.collection/find
(declare fields-to-db-object)
(defn ^DBCursor find (defn ^DBCursor find
([^String collection] ([^String collection]
@ -46,9 +47,7 @@
(.find coll (to-db-object ref)))) (.find coll (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)
n (count fields) map-of-fields (fields-to-db-object fields)]
ones (replicate n 1)
map-of-fields (zipmap fields ones)]
(.find coll (to-db-object ref) (to-db-object map-of-fields)))) (.find coll (to-db-object ref) (to-db-object map-of-fields))))
) )
@ -59,9 +58,7 @@
(.findOne coll (to-db-object { :_id id })))) (.findOne coll (to-db-object { :_id id }))))
([^String collection, ^String id, ^List fields] ([^String collection, ^String id, ^List fields]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection) (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)
n (count fields) map-of-fields (fields-to-db-object fields)]
ones (replicate n 1)
map-of-fields (zipmap fields ones)]
(.findOne coll (to-db-object { :_id id }) (to-db-object map-of-fields)))) (.findOne coll (to-db-object { :_id id }) (to-db-object map-of-fields))))
) )
@ -89,4 +86,12 @@
) )
;; monger.collection/ensure-index ;; monger.collection/ensure-index
;; monger.collection/drop-index ;; monger.collection/drop-index
(defn- fields-to-db-object
[fields]
(let [n (clojure.core/count fields)
ones (replicate n 1)
map-of-fields (zipmap fields ones)]
map-of-fields))