mirror of
https://github.com/pocoproject/poco.git
synced 2025-04-01 09:24:55 +02:00
Add more samples
This commit is contained in:
parent
e79f75d9fe
commit
7600eed502
@ -37,19 +37,41 @@
|
|||||||
#include "Poco/MongoDB/Database.h"
|
#include "Poco/MongoDB/Database.h"
|
||||||
#include "Poco/MongoDB/Cursor.h"
|
#include "Poco/MongoDB/Cursor.h"
|
||||||
|
|
||||||
// INSERT INTO USERS
|
// INSERT INTO players
|
||||||
// VALUES( "Braem", 33)
|
// VALUES( "Messi", "Lionel", 1987)
|
||||||
void sample1(Poco::MongoDB::Connection& connection)
|
void sample1(Poco::MongoDB::Connection& connection)
|
||||||
{
|
{
|
||||||
std::cout << "*** SAMPLE 1 ***" << std::endl;
|
std::cout << "*** SAMPLE 1 ***" << std::endl;
|
||||||
|
|
||||||
Poco::MongoDB::Database db("mydb");
|
Poco::MongoDB::Database db("sample");
|
||||||
|
Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertUserRequest = db.createInsertRequest("players");
|
||||||
|
|
||||||
|
// With one insert request, we can add multiple documents
|
||||||
|
|
||||||
|
Poco::MongoDB::Document::Ptr player = new Poco::MongoDB::Document();
|
||||||
|
player->add("lastname", "Messi");
|
||||||
|
player->add("firstname", "Lionel");
|
||||||
|
player->add("birthyear", 1987);
|
||||||
|
insertUserRequest->documents().push_back(player);
|
||||||
|
|
||||||
|
player = new Poco::MongoDB::Document();
|
||||||
|
player->add("lastname", "Valdes");
|
||||||
|
player->add("firstname", "Victor");
|
||||||
|
player->add("birthyear", 1982);
|
||||||
|
insertUserRequest->documents().push_back(player);
|
||||||
|
|
||||||
|
player = new Poco::MongoDB::Document();
|
||||||
|
player->add("lastname", "Puyol");
|
||||||
|
player->add("firstname", "Carles");
|
||||||
|
player->add("birthyear", 1978);
|
||||||
|
insertUserRequest->documents().push_back(player);
|
||||||
|
|
||||||
|
player = new Poco::MongoDB::Document();
|
||||||
|
player->add("lastname", "Piqué");
|
||||||
|
player->add("firstname", "Gerard");
|
||||||
|
player->add("birthyear", 1987);
|
||||||
|
insertUserRequest->documents().push_back(player);
|
||||||
|
|
||||||
Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertUserRequest = db.createInsertRequest("users");
|
|
||||||
Poco::MongoDB::Document::Ptr user = new Poco::MongoDB::Document();
|
|
||||||
user->add("lastname", std::string("Braem"));
|
|
||||||
user->add("age", 33);
|
|
||||||
insertUserRequest->documents().push_back(user);
|
|
||||||
connection.sendRequest(*insertUserRequest);
|
connection.sendRequest(*insertUserRequest);
|
||||||
std::string lastError = db.getLastError(connection);
|
std::string lastError = db.getLastError(connection);
|
||||||
if ( !lastError.empty() )
|
if ( !lastError.empty() )
|
||||||
@ -58,22 +80,22 @@ void sample1(Poco::MongoDB::Connection& connection)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SELECT lastname, age FROM users
|
// SELECT lastname, birthyear FROM players
|
||||||
void sample2(Poco::MongoDB::Connection& connection)
|
void sample2(Poco::MongoDB::Connection& connection)
|
||||||
{
|
{
|
||||||
std::cout << "*** SAMPLE 2 ***" << std::endl;
|
std::cout << "*** SAMPLE 2 ***" << std::endl;
|
||||||
|
|
||||||
Poco::MongoDB::Cursor cursor("mydb", "users");
|
Poco::MongoDB::Cursor cursor("sample", "players");
|
||||||
// Selecting fields is done by adding them to the returnFieldSelector
|
// Selecting fields is done by adding them to the returnFieldSelector
|
||||||
// Use 1 as value of the element.
|
// Use 1 as value of the element.
|
||||||
cursor.query().returnFieldSelector().add("lastname", 1);
|
cursor.query().returnFieldSelector().add("lastname", 1);
|
||||||
cursor.query().returnFieldSelector().add("age", 1);
|
cursor.query().returnFieldSelector().add("birthyear", 1);
|
||||||
Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
|
Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
for(Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
|
for(Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
|
||||||
{
|
{
|
||||||
std::cout << "LastName: " << (*it)->get<std::string>("lastname") << " Age: " << (*it)->get<int>("age") << std::endl;
|
std::cout << (*it)->get<std::string>("lastname") << " (" << (*it)->get<int>("birthyear") << ')' << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// When the cursorID is 0, there are no documents left, so break out ...
|
// When the cursorID is 0, there are no documents left, so break out ...
|
||||||
@ -87,18 +109,18 @@ void sample2(Poco::MongoDB::Connection& connection)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SELECT * FROM users
|
// SELECT * FROM players
|
||||||
void sample3(Poco::MongoDB::Connection& connection)
|
void sample3(Poco::MongoDB::Connection& connection)
|
||||||
{
|
{
|
||||||
std::cout << "*** SAMPLE 3 ***" << std::endl;
|
std::cout << "*** SAMPLE 3 ***" << std::endl;
|
||||||
|
|
||||||
Poco::MongoDB::Cursor cursor("mydb", "users");
|
Poco::MongoDB::Cursor cursor("sample", "players");
|
||||||
Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
|
Poco::MongoDB::ResponseMessage& response = cursor.next(connection);
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
for(Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
|
for(Poco::MongoDB::Document::Vector::const_iterator it = response.documents().begin(); it != response.documents().end(); ++it)
|
||||||
{
|
{
|
||||||
std::cout << "LastName: " << (*it)->get<std::string>("lastname") << " Age: " << (*it)->get<int>("age") << std::endl;
|
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 ...
|
// When the cursorID is 0, there are no documents left, so break out ...
|
||||||
@ -112,12 +134,112 @@ void sample3(Poco::MongoDB::Connection& connection)
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// SELECT * FROM players WHERE birthyear = 1978
|
||||||
|
void sample4(Poco::MongoDB::Connection& connection)
|
||||||
|
{
|
||||||
|
std::cout << "*** SAMPLE 4 ***" << std::endl;
|
||||||
|
|
||||||
|
Poco::MongoDB::Cursor cursor("sample", "players");
|
||||||
|
cursor.query().selector().add("birthyear", 1978);
|
||||||
|
|
||||||
|
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);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// SELECT * FROM players WHERE birthyear = 1987 ORDER BY name
|
||||||
|
void sample5(Poco::MongoDB::Connection& connection)
|
||||||
|
{
|
||||||
|
std::cout << "*** SAMPLE 5 ***" << std::endl;
|
||||||
|
|
||||||
|
Poco::MongoDB::Cursor cursor("sample", "players");
|
||||||
|
|
||||||
|
// When orderby is needed, use 2 separate documents in the query selector
|
||||||
|
Poco::MongoDB::Document::Ptr query = new Poco::MongoDB::Document();
|
||||||
|
query->add("birthyear", 1987);
|
||||||
|
cursor.query().selector().add("$query", query);
|
||||||
|
|
||||||
|
Poco::MongoDB::Document::Ptr order = new Poco::MongoDB::Document();
|
||||||
|
order->add("lastname", 0);
|
||||||
|
cursor.query().selector().add("$orderby", order);
|
||||||
|
|
||||||
|
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);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// SELECT * FROM players WHERE birthyear > 1969 and birthyear <= 1980
|
||||||
|
void sample6(Poco::MongoDB::Connection& connection)
|
||||||
|
{
|
||||||
|
std::cout << "*** SAMPLE 6 ***" << std::endl;
|
||||||
|
|
||||||
|
Poco::MongoDB::Cursor cursor("sample", "players");
|
||||||
|
|
||||||
|
Poco::MongoDB::Document::Ptr oper = new Poco::MongoDB::Document();
|
||||||
|
oper->add("$gt", 1969);
|
||||||
|
oper->add("$lte", 1980);
|
||||||
|
|
||||||
|
cursor.query().selector().add("birthyear", oper);
|
||||||
|
|
||||||
|
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)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
Poco::MongoDB::Connection connection("localhost", 27017);
|
Poco::MongoDB::Connection connection("localhost", 27017);
|
||||||
sample1(connection);
|
sample1(connection);
|
||||||
sample2(connection);
|
sample2(connection);
|
||||||
sample3(connection);
|
sample3(connection);
|
||||||
|
sample4(connection);
|
||||||
|
sample5(connection);
|
||||||
|
sample6(connection);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user