From 79e3f8ad2019acd3488716a577c8d9c0d7aafb94 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Tue, 16 Aug 2011 01:36:06 +0400 Subject: [PATCH] Implement monger.collection/update --- src/monger/collection.clj | 7 +++++++ test/monger/test/collection.clj | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 8dabfcf..32cfdbe 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -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 diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index 0e32620..0eb7b69 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -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" })))))