Fixes #77: Add update-by-ids function to mongo.collection
This commit is contained in:
parent
696a5ad8e3
commit
1c62587165
2 changed files with 24 additions and 1 deletions
|
|
@ -284,6 +284,21 @@
|
||||||
false
|
false
|
||||||
write-concern)))
|
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
|
;; monger.collection/save
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -163,7 +163,15 @@
|
||||||
(mc/insert-batch db coll batch)
|
(mc/insert-batch db coll batch)
|
||||||
(is (= "bed" (:type (mc/find-one-as-map db coll {:_id 2}))))
|
(is (= "bed" (:type (mc/find-one-as-map db coll {:_id 2}))))
|
||||||
(mc/update-by-id db coll 2 {"$set" {:type "living room"}})
|
(mc/update-by-id db coll 2 {"$set" {:type "living room"}})
|
||||||
(is (= "living room" (:type (mc/find-one-as-map db coll {:_id 2}))))))
|
(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
|
;; miscellenous
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue