Merge pull request #90 from Skinney/issue-77

Fix for Issue 77
This commit is contained in:
Michael Klishin 2014-10-21 09:34:43 +04:00
commit 194f0028a4
2 changed files with 50 additions and 0 deletions

View file

@ -284,6 +284,21 @@
false
write-concern)))
(defn ^WriteResult update-by-ids
"Update documents by given ids"
([^DB db ^String coll ids ^Map document]
(update-by-ids db coll ids document {}))
([^DB db ^String coll ids ^Map document {:keys [upsert write-concern]
:or {upsert false
write-concern mc/*mongodb-write-concern*}}]
(check-not-nil! (seq ids) "ids must not be nil or empty")
(.update (.getCollection db (name coll))
(to-db-object {:_id {"$in" ids}})
(to-db-object document)
upsert
true
write-concern)))
;; monger.collection/save

View file

@ -137,6 +137,41 @@
(is (= ["CA" "IL" "NY"] (sort (mc/distinct db collection :state {}))))
(is (= ["CA" "NY"] (sort (mc/distinct db collection :state {:price {$gt 100.00}}))))))
;;
;; update
;;
(let [coll "things"
batch [{:_id 1 :type "rock" :size "small"}
{:_id 2 :type "bed" :size "bed-sized"}
{:_id 3 :type "bottle" :size "1.5 liters"}]]
(deftest test-update
(mc/insert-batch db coll batch)
(is (= "small" (:size (mc/find-one-as-map db coll {:type "rock"}))))
(mc/update db coll {:type "rock"} {"$set" {:size "huge"}})
(is (= "huge" (:size (mc/find-one-as-map db coll {:type "rock"})))))
(deftest test-upsert
(is (mc/empty? db coll))
(mc/upsert db coll {:_id 4} {"$set" {:size "tiny"}})
(is (not (mc/empty? db coll)))
(mc/upsert db coll {:_id 4} {"$set" {:size "big"}})
(is (= [{:_id 4 :size "big"}] (mc/find-maps db coll {:_id 4}))))
(deftest test-update-by-id
(mc/insert-batch db coll batch)
(is (= "bed" (:type (mc/find-one-as-map db coll {:_id 2}))))
(mc/update-by-id db coll 2 {"$set" {:type "living room"}})
(is (= "living room" (:type (mc/find-one-as-map db coll {:_id 2})))))
(deftest test-update-by-ids
(mc/insert-batch db coll batch)
(is (= "bed" (:type (mc/find-one-as-map db coll {:_id 2}))))
(is (= "bottle" (:type (mc/find-one-as-map db coll {:_id 3}))))
(mc/update-by-ids db coll [2 3] {"$set" {:type "dog"}})
(is (= "dog" (:type (mc/find-one-as-map db coll {:_id 2}))))
(is (= "dog" (:type (mc/find-one-as-map db coll {:_id 3}))))))
;;
;; miscellenous