diff --git a/MongoDB/include/Poco/MongoDB/Database.h b/MongoDB/include/Poco/MongoDB/Database.h index d16248b1c..9f4a21c5b 100644 --- a/MongoDB/include/Poco/MongoDB/Database.h +++ b/MongoDB/include/Poco/MongoDB/Database.h @@ -80,6 +80,14 @@ public: /// The collectionname must not contain the database name! + Document::Ptr getLastErrorDoc(Connection& connection) const; + /// Sends the getLastError command to the database and returns the document + + + std::string getLastError(Connection& connection) const; + /// Sends the getLastError command to the database and returns the err element + /// from the error document. When err is null, an empty string is returned. + private: std::string _dbname; }; diff --git a/MongoDB/src/Database.cpp b/MongoDB/src/Database.cpp index b43ebb694..e5be810f6 100644 --- a/MongoDB/src/Database.cpp +++ b/MongoDB/src/Database.cpp @@ -69,6 +69,37 @@ double Database::count(Connection& connection, const std::string& collectionName } +Document::Ptr Database::getLastErrorDoc(Connection& connection) const +{ + Document::Ptr errorDoc; + + Poco::SharedPtr request = createQueryRequest("$cmd"); + request->setNumberToReturn(1); + request->query().add("getLastError", 1); + + Poco::MongoDB::ResponseMessage response; + connection.sendRequest(*request, response); + + if ( response.documents().size() > 0 ) + { + errorDoc = response.documents()[0]; + } + + return errorDoc; +} + +std::string Database::getLastError(Connection& connection) const +{ + Document::Ptr errorDoc = getLastErrorDoc(connection); + if ( !errorDoc.isNull() && errorDoc->isType("err") ) + { + return errorDoc->get("err"); + } + + return ""; +} + + Poco::SharedPtr Database::createQueryRequest(const std::string& collectionName) const { return new Poco::MongoDB::QueryRequest(_dbname + '.' + collectionName);