diff --git a/src/monger/core.clj b/src/monger/core.clj index 4943eb1..697d3e8 100644 --- a/src/monger/core.clj +++ b/src/monger/core.clj @@ -164,3 +164,25 @@ DBCursor (count [^com.mongodb.DBCursor this] (.count this))) + +(defn ^DBObject get-last-error + "Returns the the error (if there is one) from the previous operation on this connection. + + The result of this command looks like: + + #\" + + The value for err will be null if no error occurred, or a description otherwise. + + Important note: when calling this method directly, it is undefined which connection \"getLastError\" is called on. + You may need to explicitly use a \"consistent Request\", see requestStart() For most purposes it is better not to call this method directly but instead use WriteConcern." + ([] + (.getLastError ^DB *mongodb-database*)) + ([^DB database] + (.getLastError ^DB database)) + ([^DB database ^Integer w ^Integer wtimeout ^Boolean fsync] + (.getLastError ^DB database w wtimeout fsync)) + ([^DB database ^WriteConcern write-concern] + (.getLastError ^DB database write-concern))) + + diff --git a/test/monger/test/core.clj b/test/monger/test/core.clj index 20c36c2..738d3ee 100644 --- a/test/monger/test/core.clj +++ b/test/monger/test/core.clj @@ -1,6 +1,6 @@ (ns monger.test.core (:require [monger core collection util result]) - (:import (com.mongodb Mongo DB)) + (:import (com.mongodb Mongo DB WriteConcern)) (:use [clojure.test])) @@ -42,3 +42,11 @@ { :collstats "things" :scale (* 1024 1024) } { :getLastError 1 }]] (is (monger.result/ok? (monger.core/command c)))))) + +(deftest get-last-error + (let [connection (monger.core/connect) + db (monger.core/get-db connection "monger-test")] + (is (monger.result/ok? (monger.core/get-last-error))) + (is (monger.result/ok? (monger.core/get-last-error db))) + (is (monger.result/ok? (monger.core/get-last-error db WriteConcern/NORMAL))) + (is (monger.result/ok? (monger.core/get-last-error db 1 100 true))))) \ No newline at end of file