fix sample

This commit is contained in:
Guenter Obiltschnig 2017-02-13 16:41:37 +01:00
parent 4e0b11f306
commit 5c0db0bb89

View File

@ -152,7 +152,7 @@ void sample2(Poco::MongoDB::Connection& connection)
cursor.query().returnFieldSelector().add("lastname", 1);
cursor.query().returnFieldSelector().add("birthyear", 1);
Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
while(1)
for (;;)
{
for (Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
{
@ -178,7 +178,7 @@ void sample3(Poco::MongoDB::Connection& connection)
Poco::MongoDB::Cursor cursor("sample", "players");
Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
while(1)
for (;;)
{
for (Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
{
@ -206,7 +206,7 @@ void sample4(Poco::MongoDB::Connection& connection)
cursor.query().selector().add("birthyear", 1978);
Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
while(1)
for (;;)
{
for (Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
{
@ -234,10 +234,10 @@ void sample5(Poco::MongoDB::Connection& connection)
// When orderby is needed, use 2 separate documents in the query selector
cursor.query().selector().addNewDocument("$query").add("birthyear", 1987);
cursor.query().selector().addNewDocument("$orderby").add("lastname", -1);
cursor.query().selector().addNewDocument("$orderby").add("lastname", 1);
Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
while(1)
for (;;)
{
for (Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
{
@ -268,7 +268,7 @@ void sample6(Poco::MongoDB::Connection& connection)
.add("$lte", 1980);
Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
while(1)
for (;;)
{
for (Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
{
@ -322,7 +322,7 @@ void sample8(Poco::MongoDB::Connection& connection)
cursor.query().setNumberToReturn(10);
cursor.query().setNumberToSkip(20);
Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
while(1)
for (;;)
{
for (Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
{
@ -410,7 +410,7 @@ void sample11(Poco::MongoDB::Connection& connection)
if (response.hasDocuments())
{
std::cout << "Count: " << response.documents()[0]->get<int>("n") << std::endl;
std::cout << "Count: " << response.documents()[0]->getInteger("n") << std::endl;
}
}
@ -448,40 +448,13 @@ void sample13(Poco::MongoDB::Connection& connection)
std::cout << "LastError: " << lastError->toString(2) << std::endl;
}
//SELECT players WHERE birthyear IN (1982, 1983)
void sample14(Poco::MongoDB::Connection& connection)
{
std::cout << "*** SAMPLE 14 ***" << std::endl;
Poco::MongoDB::Cursor cursor("sample", "players");
Poco::MongoDB::Array::Ptr years = new Poco::MongoDB::Array();
years->add("0", 1982);
years->add("1", 1983);
cursor.query().selector().addNewDocument("birthyear").add("$in", years);
Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
while(1)
{
for(Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
{
std::cout << (*it)->get<std::string>("lastname") << ' ' << (*it)->get<std::string>("firstname") << " (" << (*it)->get<int>("birthyear") << ')' << std::endl;
}
// When the cursorID is 0, there are no documents left, so break out ...
if ( response.cursorID() == 0 )
{
break;
}
// Get the next bunch of documents
response = cursor.next(connection);
};
}
int main(int argc, char** argv)
{
Poco::MongoDB::Connection connection("localhost", 27017);
try
{
sample1(connection);
sample2(connection);
sample3(connection);
@ -495,7 +468,11 @@ int main(int argc, char** argv)
sample11(connection);
sample12(connection);
sample13(connection);
sample14(connection);
}
catch (Poco::Exception& exc)
{
std::cerr << exc.displayText() << std::endl;
}
return 0;
}