Skip to content

Commit aaabc3c

Browse files
committed
Database::count() can parse different types of 'n' attribute in result.
1 parent 0a68bbf commit aaabc3c

3 files changed

Lines changed: 15 additions & 6 deletions

File tree

MongoDB/include/Poco/MongoDB/Database.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class MongoDB_API Database
4545
virtual ~Database();
4646
/// Destructor
4747

48-
int count(Connection& connection, const std::string& collectionName) const;
48+
Int64 count(Connection& connection, const std::string& collectionName) const;
4949
/// Sends a count request for the given collection to MongoDB. When
5050
/// the command fails, -1 is returned.
5151

MongoDB/src/Database.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace Poco {
2323
namespace MongoDB {
2424

2525

26-
Database::Database( const std::string& db) : _dbname(db)
26+
Database::Database(const std::string& db) : _dbname(db)
2727
{
2828
}
2929

@@ -32,7 +32,7 @@ Database::~Database()
3232
}
3333

3434

35-
int Database::count(Connection& connection, const std::string& collectionName) const
35+
Int64 Database::count(Connection& connection, const std::string& collectionName) const
3636
{
3737
Poco::SharedPtr<Poco::MongoDB::QueryRequest> countRequest = createCountRequest(collectionName);
3838

@@ -42,7 +42,16 @@ int Database::count(Connection& connection, const std::string& collectionName) c
4242
if ( response.documents().size() > 0 )
4343
{
4444
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
45-
return doc->get<int>("n");
45+
46+
if (doc->isType<double>("n")) {
47+
return static_cast<Int64>(doc->get<double>("n"));
48+
}
49+
else if (doc->isType<Int32>("n")) {
50+
return doc->get<Int32>("n");
51+
}
52+
else if (doc->isType<Int64>("n")) {
53+
return doc->get<Int64>("n");
54+
}
4655
}
4756

4857
return -1;

MongoDB/testsuite/src/MongoDBTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ void MongoDBTest::testDBCount2Command()
268268
}
269269

270270
Poco::MongoDB::Database db("team");
271-
int count = db.count(_mongo, "players");
271+
Poco::Int64 count = db.count(_mongo, "players");
272272
assert(count == 1);
273273
}
274274

@@ -306,7 +306,7 @@ void MongoDBTest::testCursorRequest()
306306
}
307307
_mongo.sendRequest(*insertRequest);
308308

309-
int count = db.count(_mongo, "numbers");
309+
Poco::Int64 count = db.count(_mongo, "numbers");
310310
std::cout << "count= " << count << std::endl;
311311
assert(count == 10000);
312312

0 commit comments

Comments
 (0)