diff --git a/project.clj b/project.clj index 9443f99..27b89db 100644 --- a/project.clj +++ b/project.clj @@ -1,11 +1,11 @@ -(defproject helpshift/mongrove "0.2.0" +(defproject helpshift/mongrove "0.2.1" :description "An idiomatic Clojure wrapper for MongoDB java-driver 4.x" :url "https://medium.com/helpshift-engineering" :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" :url "https://www.eclipse.org/legal/epl-2.0/"} :signing {:gpg-key "helpshift-clojars-admin"} :dependencies [[org.clojure/clojure "1.10.0"] - [org.mongodb/mongodb-driver-sync "4.1.1"] + [org.mongodb/mongodb-driver-sync "4.2.3"] [org.clojure/tools.logging "1.1.0"]] :repl-options {:init-ns mongrove.core} :jvm-opts ^:replace ["-Duser.timezone=UTC" diff --git a/src/mongrove/core.clj b/src/mongrove/core.clj index ac487fa..a7be25c 100644 --- a/src/mongrove/core.clj +++ b/src/mongrove/core.clj @@ -11,6 +11,7 @@ ClientSessionOptions$Builder MongoClientSettings MongoClientSettings$Builder + MongoCredential ReadConcern ReadPreference ServerAddress @@ -20,6 +21,7 @@ (com.mongodb.client ClientSession FindIterable + DistinctIterable MongoClient MongoClients MongoCollection @@ -167,7 +169,8 @@ "Initialize a ConnectionPoolSettings object from given options map. Available options : :read-preference :read-concern :write-concern :retry-reads :retry-writes" - [{:keys [read-preference read-concern write-concern + [{{:keys [user-name password source]} :credential + :keys [read-preference read-concern write-concern retry-reads retry-writes] :as opts}] {:pre [(or (nil? read-preference) (read-preference-map read-preference)) @@ -178,6 +181,7 @@ (let [opts (merge default-opts opts) {:keys [read-preference read-concern write-concern retry-reads retry-writes]} opts + credential (MongoCredential/createCredential user-name source (char-array password)) builder (doto (MongoClientSettings/builder) (socket-settings opts) (cluster-settings opts) @@ -186,6 +190,7 @@ (.writeConcern (get write-concern-map write-concern)) (.readPreference (get read-preference-map read-preference)) (.retryWrites retry-writes) + (.credential credential) ;; @TODO : Documentation states that this method ;; exists ! yet we get method not found exception #_(.retryReads retry-reads))] @@ -367,6 +372,23 @@ (.countDocuments collection session bson-query) (.countDocuments collection bson-query))))) +(defn ^:public-api distinct-vals + "Gets the distinct values for a field name that matches the filter." + ([^MongoDatabase db + ^String coll + ^String field-name + ^Class class-type + filter & {:keys [only exclude session] + :or {only [] exclude []}}] + (let [collection (get-collection db coll) + bson-filter (conversion/to-bson-document filter) + ^DistinctIterable iterator + (if session + (.distinct ^MongoCollection collection session field-name bson-filter class-type) + (.distinct ^MongoCollection collection field-name bson-filter class-type)) + cursor (.cursor iterator)] + (iterator-seq cursor)))) + (defn ^:public-api delete "Delete a document from the collection that matches `query`.