Implement monger.collection/update

This commit is contained in:
Michael S. Klishin 2011-08-16 01:36:06 +04:00
parent b3d82ce3a2
commit 79e3f8ad20
2 changed files with 39 additions and 0 deletions

View file

@ -76,6 +76,13 @@
(.count coll (to-db-object conditions)))))
;; monger.collection/update
(defn ^WriteResult update
[^String collection, ^Map conditions, ^Map document, & { :keys [upsert multi write-concern] :or { upsert false, multi false, write-concern monger.core/*mongodb-write-concern* } }]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.update coll (to-db-object conditions) (to-db-object document) upsert multi write-concern)))
;; monger.collection/update-multi
;; monger.collection/remove

View file

@ -169,3 +169,35 @@
(let [doc (monger.convertion/from-db-object i true)]
(is (= (:language doc) "Clojure"))))
(is (empty? (monger.collection/find collection { :language "Erlang" } [:name]))))))
;;
;; update
;;
(deftest update-document-by-id-without-upsert
(let [collection "libraries"
doc-id (monger.util/random-uuid)
doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }
modified-doc { :data-store "MongoDB", :language "Erlang", :_id doc-id }]
(monger.collection/remove collection)
(monger.collection/insert collection doc)
(is (= (doc (monger.collection/find-by-id collection doc-id))))
(monger.collection/update collection { :_id doc-id } { :language "Erlang" })
(is (= (modified-doc (monger.collection/find-by-id collection doc-id))))))
(deftest update-multiple-documents
(let [collection "libraries"]
(monger.collection/remove collection)
(monger.collection/insert collection { :language "Clojure", :name "monger" })
(monger.collection/insert collection { :language "Clojure", :name "langohr" })
(monger.collection/insert collection { :language "Clojure", :name "incanter" })
(monger.collection/insert collection { :language "Scala", :name "akka" })
(is (= 3 (monger.collection/count collection { :language "Clojure" })))
(is (= 1 (monger.collection/count collection { :language "Scala" })))
(is (= 0 (monger.collection/count collection { :language "Python" })))
(monger.collection/update collection { :language "Clojure" } { "$set" { :language "Python" } } :multi true)
(is (= 0 (monger.collection/count collection { :language "Clojure" })))
(is (= 1 (monger.collection/count collection { :language "Scala" })))
(is (= 3 (monger.collection/count collection { :language "Python" })))))