Database::count() can parse different types of 'n' attribute in result.

This commit is contained in:
fbraem 2015-06-03 20:34:05 +02:00
parent 0a68bbfbea
commit aaabc3c9e9
3 changed files with 15 additions and 6 deletions

View File

@ -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.

View File

@ -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<Poco::MongoDB::QueryRequest> 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<int>("n");
if (doc->isType<double>("n")) {
return static_cast<Int64>(doc->get<double>("n"));
}
else if (doc->isType<Int32>("n")) {
return doc->get<Int32>("n");
}
else if (doc->isType<Int64>("n")) {
return doc->get<Int64>("n");
}
}
return -1;

View File

@ -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);