diff --git a/ChangeLog.md b/ChangeLog.md index 87deea4..253ccc3 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -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 diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 48e11c6..bf209eb 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -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 diff --git a/test/monger/test/updating.clj b/test/monger/test/updating.clj index 7c0084e..df86048 100644 --- a/test/monger/test/updating.clj +++ b/test/monger/test/updating.clj @@ -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"]