Example #1
0
// SELECT DISTINCT birthyear FROM players WHERE birthyear > 1980
void sample10(Poco::MongoDB::Connection& connection)
{
	std::cout << "*** SAMPLE 10 ***" << std::endl;

	Poco::MongoDB::Database db("sample");
	Poco::SharedPtr<Poco::MongoDB::QueryRequest> command = db.createCommand();

	command->selector()
		.add("distinct", "players")
		.add("key", "birthyear")
		.addNewDocument("query")
		.addNewDocument("birthyear")
		.add("$gt", 1980);

	Poco::MongoDB::ResponseMessage response;
	connection.sendRequest(*command, response);
	if ( response.hasDocuments() )
	{
		Poco::MongoDB::Array::Ptr values = response.documents()[0]->get<Poco::MongoDB::Array::Ptr>("values");
		for(int i = 0; i < values->size(); ++i )
		{
			std::cout << values->get<int>(i) << std::endl;
		}
	}

}
Example #2
0
// SELECT * FROM players LIMIT 1
void sample9(Poco::MongoDB::Connection& connection)
{
	std::cout << "*** SAMPLE 9 ***" << std::endl;

	// QueryRequest can be used directly
	Poco::MongoDB::QueryRequest query("sample.players");
	query.setNumberToReturn(1);
	Poco::MongoDB::ResponseMessage response;
	connection.sendRequest(query, response);
	if ( response.hasDocuments() )
	{
		std::cout << response.documents()[0]->toString(2) << std::endl;
	}

	// QueryRequest can be created using the Database class
	Poco::MongoDB::Database db("sample");
	Poco::SharedPtr<Poco::MongoDB::QueryRequest> queryPtr = db.createQueryRequest("players");
	queryPtr->setNumberToReturn(1);
	connection.sendRequest(*queryPtr, response);
	if ( response.hasDocuments() )
	{
		std::cout << response.documents()[0]->toString(2) << std::endl;
	}
}
Example #3
0
// SELECT COUNT(*) FROM players WHERE birthyear > 1980
void sample11(Poco::MongoDB::Connection& connection)
{
	std::cout << "*** SAMPLE 11 ***" << std::endl;

	Poco::MongoDB::Database db("sample");
	Poco::SharedPtr<Poco::MongoDB::QueryRequest> count = db.createCountRequest("players");
	count->selector().addNewDocument("query")
		.addNewDocument("birthyear")
			.add("$gt", 1980);

	Poco::MongoDB::ResponseMessage response;
	connection.sendRequest(*count, response);

	if ( response.hasDocuments() )
	{
		std::cout << "Count: " << response.documents()[0]->get<double>("n") << std::endl;
	}
}