Add monger.collection/upsert

This commit is contained in:
Michael Klishin 2013-04-15 21:24:08 +04:00
parent 879a0232b0
commit c0a83e7241
2 changed files with 25 additions and 1 deletions

View file

@ -404,6 +404,17 @@
multi
write-concern)))
(defn ^WriteResult upsert
"Performs an upsert.
This is a convenience function that delegates to monger.collection/update and
sets :upsert to true.
See monger.collection/update documentation"
[^String collection ^Map conditions ^Map document & {:keys [upsert multi write-concern] :or {multi false
write-concern monger.core/*mongodb-write-concern*}}]
(update collection conditions document :multi multi :write-concern write-concern))
(defn ^WriteResult update-by-id
"Update a document with given id"
[^String collection id ^Map document & {:keys [upsert write-concern] :or {upsert false

View file

@ -137,7 +137,7 @@
(deftest ^{:updating true} upsert-a-document
(deftest ^{:updating true} upsert-a-document-using-update
(let [collection "libraries"
doc-id (monger.util/random-uuid)
date (Date.)
@ -149,3 +149,16 @@
(is (= 1 (mc/count collection)))
(is (= (modified-doc (mc/find-by-id collection doc-id))))
(mc/remove collection)))
(deftest ^{:updating true} upsert-a-document-using-upsert
(let [collection "libraries"
doc-id (monger.util/random-uuid)
date (Date.)
doc {:created-at date :data-store "MongoDB" :language "Clojure" :_id doc-id}
modified-doc {:created-at date :data-store "MongoDB" :language "Erlang" :_id doc-id}]
(is (not (monger.result/updated-existing? (mc/upsert collection {:language "Clojure"} doc))))
(is (= 1 (mc/count collection)))
(is (monger.result/updated-existing? (mc/upsert collection {:language "Clojure"} modified-doc :multi false)))
(is (= 1 (mc/count collection)))
(is (= (modified-doc (mc/find-by-id collection doc-id))))
(mc/remove collection)))