From aaabc3c9e9598feaea9db967e7d2c73e06caa8b6 Mon Sep 17 00:00:00 2001 From: fbraem Date: Wed, 3 Jun 2015 20:34:05 +0200 Subject: [PATCH] Database::count() can parse different types of 'n' attribute in result. --- MongoDB/include/Poco/MongoDB/Database.h | 2 +- MongoDB/src/Database.cpp | 15 ++++++++++++--- MongoDB/testsuite/src/MongoDBTest.cpp | 4 ++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/MongoDB/include/Poco/MongoDB/Database.h b/MongoDB/include/Poco/MongoDB/Database.h index 946df111e..f58bfea71 100644 --- a/MongoDB/include/Poco/MongoDB/Database.h +++ b/MongoDB/include/Poco/MongoDB/Database.h @@ -45,7 +45,7 @@ public: virtual ~Database(); /// Destructor - int count(Connection& connection, const std::string& collectionName) const; + Int64 count(Connection& connection, const std::string& collectionName) const; /// Sends a count request for the given collection to MongoDB. When /// the command fails, -1 is returned. diff --git a/MongoDB/src/Database.cpp b/MongoDB/src/Database.cpp index 27186c317..6a3598668 100644 --- a/MongoDB/src/Database.cpp +++ b/MongoDB/src/Database.cpp @@ -23,7 +23,7 @@ namespace Poco { namespace MongoDB { -Database::Database( const std::string& db) : _dbname(db) +Database::Database(const std::string& db) : _dbname(db) { } @@ -32,7 +32,7 @@ Database::~Database() } -int Database::count(Connection& connection, const std::string& collectionName) const +Int64 Database::count(Connection& connection, const std::string& collectionName) const { Poco::SharedPtr countRequest = createCountRequest(collectionName); @@ -42,7 +42,16 @@ int Database::count(Connection& connection, const std::string& collectionName) c if ( response.documents().size() > 0 ) { Poco::MongoDB::Document::Ptr doc = response.documents()[0]; - return doc->get("n"); + + if (doc->isType("n")) { + return static_cast(doc->get("n")); + } + else if (doc->isType("n")) { + return doc->get("n"); + } + else if (doc->isType("n")) { + return doc->get("n"); + } } return -1; diff --git a/MongoDB/testsuite/src/MongoDBTest.cpp b/MongoDB/testsuite/src/MongoDBTest.cpp index d7156009a..35d1d1d23 100644 --- a/MongoDB/testsuite/src/MongoDBTest.cpp +++ b/MongoDB/testsuite/src/MongoDBTest.cpp @@ -268,7 +268,7 @@ void MongoDBTest::testDBCount2Command() } Poco::MongoDB::Database db("team"); - int count = db.count(_mongo, "players"); + Poco::Int64 count = db.count(_mongo, "players"); assert(count == 1); } @@ -306,7 +306,7 @@ void MongoDBTest::testCursorRequest() } _mongo.sendRequest(*insertRequest); - int count = db.count(_mongo, "numbers"); + Poco::Int64 count = db.count(_mongo, "numbers"); std::cout << "count= " << count << std::endl; assert(count == 10000);