From 2a1e52fc1e8cd3261fbece7f0aa30e00e94cb52e Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Wed, 11 Jul 2012 22:26:13 +0400 Subject: [PATCH] Make sure monger.collection/insert-and-return respects existing document id, just like save-and-return does --- src/clojure/monger/collection.clj | 2 +- test/monger/test/inserting_test.clj | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/clojure/monger/collection.clj b/src/clojure/monger/collection.clj index 9950564..17d15ab 100644 --- a/src/clojure/monger/collection.clj +++ b/src/clojure/monger/collection.clj @@ -97,7 +97,7 @@ ;; and it does not work very well in our case, because that DBObject is short lived and produced ;; from the Clojure map we are passing in. Plus, this approach is very awkward with immutable data ;; structures being the default. MK. - (let [doc (merge document {:_id (ObjectId.)})] + (let [doc (merge {:_id (ObjectId.)} document)] (insert db collection doc concern) doc))) diff --git a/test/monger/test/inserting_test.clj b/test/monger/test/inserting_test.clj index 5f0ad0d..a687180 100644 --- a/test/monger/test/inserting_test.clj +++ b/test/monger/test/inserting_test.clj @@ -129,6 +129,13 @@ (is (:_id result)) (is (= 1 (mc/count collection))))) +(deftest insert-and-return-with-a-provided-id + (let [collection "people" + oid (ObjectId.) + doc {:name "Joe" :age 30 :_id oid} + result (mc/insert-and-return :people doc)] + (is (= (:_id result) (:_id doc) oid)) + (is (= 1 (mc/count collection))))) ;;