Introduce monger.collection/update-by-id
This commit is contained in:
parent
6c1dcdcc89
commit
fdcad90cd8
3 changed files with 30 additions and 3 deletions
|
|
@ -53,3 +53,9 @@ monger.collection/find-map-by-id no longer ignore fields argument. Contributed b
|
|||
|
||||
`monger.db` namespace was added to perform operations like adding users or dropping databases. Several functions from
|
||||
`monger.core` will eventually be moved there, but not for 1.0. Contributed by Toby Hede.
|
||||
|
||||
|
||||
### monger.collection/update-by-id
|
||||
|
||||
monger.collection/update-by-id is a new convenience function for updating a single document with
|
||||
given ObjectId
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@
|
|||
(:refer-clojure :exclude [find remove count drop distinct empty?])
|
||||
(:import [com.mongodb Mongo DB DBCollection WriteResult DBObject WriteConcern DBCursor MapReduceCommand MapReduceCommand$OutputType]
|
||||
[java.util List Map]
|
||||
[clojure.lang IPersistentMap ISeq])
|
||||
[clojure.lang IPersistentMap ISeq]
|
||||
[org.bson.types ObjectId])
|
||||
(:require [monger core result])
|
||||
(:use [monger.conversion]))
|
||||
|
||||
|
|
@ -307,9 +308,18 @@
|
|||
(monger.collection/update \"people\" { :first_name \"Yoko\" } { :first_name \"Yoko\" :last_name \"Ono\" } :upsert true)
|
||||
|
||||
By default :upsert and :multi are false."
|
||||
[^String collection ^Map conditions ^Map document & { :keys [upsert multi write-concern] :or { upsert false, multi false, write-concern monger.core/*mongodb-write-concern* } }]
|
||||
([^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))))
|
||||
|
||||
(defn ^WriteResult update-by-id
|
||||
"Update a document with given id"
|
||||
[^String collection ^ObjectId id ^Map document & { :keys [upsert write-concern] :or { upsert 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)))
|
||||
(.update coll (to-db-object { :_id id }) (to-db-object document) upsert false write-concern)))
|
||||
|
||||
|
||||
;; monger.collection/save
|
||||
|
|
|
|||
|
|
@ -33,6 +33,17 @@
|
|||
(mgcol/update collection { :_id doc-id } { :language "Erlang" })
|
||||
(is (= (modified-doc (mgcol/find-by-id collection doc-id))))))
|
||||
|
||||
(deftest update-document-by-id-without-upsert-using-update-by-id
|
||||
(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 }]
|
||||
(mgcol/insert collection doc)
|
||||
(is (= (doc (mgcol/find-by-id collection doc-id))))
|
||||
(mgcol/update-by-id collection doc-id { :language "Erlang" })
|
||||
(is (= (modified-doc (mgcol/find-by-id collection doc-id))))))
|
||||
|
||||
|
||||
(deftest update-multiple-documents
|
||||
(let [collection "libraries"]
|
||||
|
|
|
|||
Loading…
Reference in a new issue