diff --git a/CHANGELOG.md b/CHANGELOG.md index 00e033c..b20ce05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ # Change Log All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). +## 0.7.0 - 2022-10-04 +### Added +- Support reading dates as instances (thanks @henryw374) +- Support data literal for mongo id (thanks @henryw374) +- Support for implicit transactions (thanks @AdamClements) +- Support for aggregation pipeline in find-one-and-update (requires 4.2+, thanks @jacobemcken) + ## 0.6.0 - 2020-01-10 ### Added - Support for bulk-write diff --git a/README.md b/README.md index ffa3743..be023e9 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ It was developed with the following goals: ## Status -mongo-driver-3 is used in production, but is also under development and the API may change slightly. +mongo-driver-3 is used in production, and the existing public API will be maintained. Please try it out and raise any issues you may find. ## Usage @@ -32,10 +32,10 @@ For Leinengen, add this to your project.clj: ```clojure ;; The underlying driver -- any newer version can also be used -[org.mongodb/mongodb-driver-sync "3.11.2"] +[org.mongodb/mongodb-driver-sync "4.2.3"] ;; This wrapper library -[mongo-driver-3 "0.5.0"] +[mongo-driver-3 "0.6.0"] ``` ## Getting started @@ -171,6 +171,14 @@ use `with-open` so the session is closed after both successful and failed transa (fn [] (mc/insert-one my-db "coll" {:name "hello"} {:session s}) (mc/insert-one my-db "coll" {:name "world"} {:session s})))) + +;; There is also a helper method to make this easier, +;; where it is not necessary to manually open or pass a session: +(mg/with-implicit-transaction + {:client client} + (fn [] + (mc/insert-one my-db "coll" {:name "hello"}) + (mc/insert-one my-db "coll" {:name "world"}))) ``` ## License diff --git a/project.clj b/project.clj index fe12959..dd09808 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject mongo-driver-3 "0.6.1-SNAPSHOT" +(defproject mongo-driver-3 "0.7.0-SNAPSHOT" :description "A Clojure wrapper for the Java MongoDB driver 3.11/4.0+." :url "https://github.com/gnarroway/mongo-driver-3" :license {:name "The MIT License" diff --git a/src/mongo_driver_3/client.clj b/src/mongo_driver_3/client.clj index f4de099..747ba45 100644 --- a/src/mongo_driver_3/client.clj +++ b/src/mongo_driver_3/client.clj @@ -150,7 +150,24 @@ (defn with-implicit-transaction "Automatically sets the session / transaction for all mongo operations - within the scope, using a dynamic binding" + within the scope, using a dynamic binding. + + The first argument is an options map with keys: + + - `:client` a MongoClient (mandatory) + - `:transaction-opts` (see `->TransactionOptions` for keys) + - `:session-opts` (see `start-session` for details) + + The second argument `body` is a fn with one or more mongo operations in it. + e.g. + + ``` + (mg/with-implicit-transaction + {:client client} + (fn [] + (mc/insert-one my-db \"coll\" {:name \"hello\"}) + (mc/insert-one my-db \"coll\" {:name \"world\"}))) + ```" [{:keys [^MongoClient client transaction-opts session-opts] :or {transaction-opts {} session-opts {}}} body] (with-open [^ClientSession session (start-session client session-opts)] (binding [*session* session] diff --git a/src/mongo_driver_3/collection.clj b/src/mongo_driver_3/collection.clj index e9c1518..f9cbb30 100644 --- a/src/mongo_driver_3/collection.clj +++ b/src/mongo_driver_3/collection.clj @@ -5,8 +5,7 @@ (:import (com.mongodb MongoNamespace) (com.mongodb.client MongoDatabase MongoCollection ClientSession) (com.mongodb.client.model IndexModel) - (java.util List) - (org.bson Document))) + (java.util List))) (set! *warn-on-reflection* true) diff --git a/test/mongo_driver_3/collection_test.clj b/test/mongo_driver_3/collection_test.clj index 7092f4b..bbf356d 100644 --- a/test/mongo_driver_3/collection_test.clj +++ b/test/mongo_driver_3/collection_test.clj @@ -216,7 +216,13 @@ (let [db (new-db @client)] (is (nil? (dissoc (mc/find-one-and-update db "test" {:id 1} {:$set {:r 1}} {:return-new? true}) :_id))) - (is (= {:id 1 :r 1} (dissoc (mc/find-one-and-update db "test" {:id 1} {:$set {:r 1}} {:return-new? true :upsert? true}) :_id)))))) + (is (= {:id 1 :r 1} (dissoc (mc/find-one-and-update db "test" {:id 1} {:$set {:r 1}} {:return-new? true :upsert? true}) :_id))))) + + (testing "aggregation pipeline" + (let [db (new-db @client) + _ (mc/insert-many db "test" [{:id 1 :a [1 2] :b []} {:id 2 :a [7 8] :b []}])] + + (is (= {:id 1 :a [] :b [1 2]} (dissoc (mc/find-one-and-update db "test" {:id 1} [{:$set {:b :$a}} {:$set {:a []}}] {:return-new? true}) :_id)))))) (deftest ^:integration test-find-one-and-replace (testing "return new"