Пример #1
0
void MongoDBTest::testCountCommand()
{
	if (!_connected)
	{
		std::cout << "Not connected, test skipped." << std::endl;
		return;
	}

	Poco::MongoDB::QueryRequest request("team.$cmd");
	request.setNumberToReturn(1);
	request.selector().add("count", std::string("players"));

	Poco::MongoDB::ResponseMessage response;

	_mongo.sendRequest(request, response);

	if ( response.documents().size() > 0 )
	{
		Poco::MongoDB::Document::Ptr doc = response.documents()[0];
		double count = doc->get<double>("n");
		assert(count == 1);
	}
	else
	{
		fail("Didn't get a response from the count command");
	}
}
Пример #2
0
void MongoDBTest::testDBCountCommand()
{
	if (!_connected)
	{
		std::cout << "Not connected, test skipped." << std::endl;
		return;
	}

	Poco::MongoDB::Database db("team");
	Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = db.createCountRequest("players");

	Poco::MongoDB::ResponseMessage response;
	_mongo.sendRequest(*request, response);

	if ( response.documents().size() > 0 )
	{
		Poco::MongoDB::Document::Ptr doc = response.documents()[0];
		double count = doc->get<double>("n");
		assert(count == 1);
	}
	else
	{
		fail("Didn't get a response from the count command");
	}
}
Пример #3
0
void MongoDBTest::testConnectionPool()
{
	Poco::Net::SocketAddress sa("localhost", 27017);
	Poco::PoolableObjectFactory<Poco::MongoDB::Connection, Poco::MongoDB::Connection::Ptr> factory(sa);
	Poco::ObjectPool<Poco::MongoDB::Connection, Poco::MongoDB::Connection::Ptr> pool(factory, 10, 15);

	Poco::MongoDB::PooledConnection pooledConnection(pool);

	Poco::MongoDB::QueryRequest request("team.$cmd");
	request.setNumberToReturn(1);
	request.selector().add("count", std::string("players"));

	Poco::MongoDB::ResponseMessage response;
	((Connection::Ptr) pooledConnection)->sendRequest(request, response);

	if ( response.documents().size() > 0 )
	{
		Poco::MongoDB::Document::Ptr doc = response.documents()[0];
		assert(doc->getInteger("n") == 1);
	}
	else
	{
		fail("Didn't get a response from the count command");
	}
}
Пример #4
0
void MongoDBTest::testBuildInfo()
{
	Poco::MongoDB::QueryRequest request("team.$cmd");
	request.setNumberToReturn(1);
	request.selector().add("buildInfo", 1);

	Poco::MongoDB::ResponseMessage response;

	try
	{
		_mongo->sendRequest(request, response);
	}
	catch(Poco::NotImplementedException& nie)
	{
		std::cout << nie.message() << std::endl;
		return;
	}

	if ( response.documents().size() > 0 )
	{
		Poco::MongoDB::Document::Ptr doc = response.documents()[0];
		std::cout << doc->toString(2);
	}
	else
	{
		fail("Didn't get a response from the buildinfo command");
	}
}
bool MongoAuthentication::authenticateMongoCR(const std::string &user, const std::string &pwd)
{
	bool ret = false;
	_user = user;
	_pwd = pwd;

	try {
		std::string nonce;
		Poco::SharedPtr<Poco::MongoDB::QueryRequest> command = _db.createCommand();
		command->selector().add<Poco::Int32>("getnonce", 1);
		command->setNumberToReturn(1);

		Poco::MongoDB::ResponseMessage response;
		_conn.sendRequest(*command, response);

		if (response.documents().size() > 0) {
			Poco::MongoDB::Document::Ptr doc = response.documents()[0];
			nonce = doc->get<std::string>("nonce", "");

			std::string password = _user + ":mongo:" + _pwd;

			Poco::MD5Engine md5;
			md5.update(password);
			std::string hashedPassword(Poco::DigestEngine::digestToHex(md5.digest()));
			std::string key = nonce + _user + hashedPassword;
			md5.reset();
			md5.update(key);
			std::string hashedKey(Poco::DigestEngine::digestToHex(md5.digest()));


			Poco::SharedPtr<Poco::MongoDB::QueryRequest> command = _db.createCommand();
			command->selector()
				.add<Poco::Int32>("authenticate", 1)
				.add<std::string>("user", _user)
				.add<std::string>("nonce", nonce)
				.add<std::string>("key", hashedKey);      // hex_md5( n.nonce + username + hex_md5( username + ":mongo:" + password ) )

			_conn.sendRequest(*command, response);

			if (response.documents().size() > 0 && response.documents()[0]->get<double>("ok")) {
				ret = true;
			}
		}
	} catch (Poco::Exception &) {
		throw Poco::ApplicationException("auth failed.");
	}

	return ret;
}
Пример #6
0
double Database::count(Connection& connection, const std::string& collectionName) const
{
	Poco::SharedPtr<Poco::MongoDB::QueryRequest> countRequest = createCountRequest(collectionName);

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

	if ( response.documents().size() > 0 )
	{
		Poco::MongoDB::Document::Ptr doc = response.documents()[0];
		return doc->get<double>("n");
	}

	return -1;
}
Пример #7
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;
		}
	}

}
Пример #8
0
void MongoDBTest::testUUID()
{
	Poco::MongoDB::Document::Ptr club = new Poco::MongoDB::Document();
	club->add("name", std::string("Barcelona"));

	Poco::UUIDGenerator generator;
	Poco::UUID uuid = generator.create();
	Poco::MongoDB::Binary::Ptr uuidBinary = new Poco::MongoDB::Binary(uuid);
	club->add("uuid", uuidBinary);

	Poco::MongoDB::InsertRequest request("team.club");
	request.documents().push_back(club);

	_mongo->sendRequest(request);

	Poco::MongoDB::QueryRequest queryReq("team.club");
	queryReq.selector().add("name" , std::string("Barcelona"));

	Poco::MongoDB::ResponseMessage response;
	_mongo->sendRequest(queryReq, response);

	if ( response.documents().size() > 0 )
	{
		Poco::MongoDB::Document::Ptr doc = response.documents()[0];

		try
		{
			std::string name = doc->get<std::string>("name");
			assert(name.compare("Barcelona") == 0);

			Poco::MongoDB::Binary::Ptr uuidBinary = doc->get<Binary::Ptr>("uuid");
			assert(uuid == uuidBinary->uuid());
		}
		catch(Poco::NotFoundException& nfe)
		{
			fail(nfe.message() + " not found.");
		}
	}
	else
	{
		fail("No document returned");
	}

	Poco::MongoDB::DeleteRequest delRequest("team.club");
	delRequest.selector().add("name", std::string("Barcelona"));
	_mongo->sendRequest(delRequest);
}
Пример #9
0
void MongoDBTest::testQueryRequest()
{
	if (!_connected)
	{
		std::cout << "Not connected, test skipped." << std::endl;
		return;
	}

	Poco::MongoDB::QueryRequest request("team.players");
	request.selector().add("lastname" , std::string("Braem"));
	request.setNumberToReturn(1);

	Poco::MongoDB::ResponseMessage response;

	_mongo.sendRequest(request, response);

	if ( response.documents().size() > 0 )
	{
		Poco::MongoDB::Document::Ptr doc = response.documents()[0];

		try
		{
			std::string lastname = doc->get<std::string>("lastname");
			assert(lastname.compare("Braem") == 0);
			std::string firstname = doc->get<std::string>("firstname");
			assert(firstname.compare("Franky") == 0);
			Poco::Timestamp birthDateTimestamp = doc->get<Poco::Timestamp>("birthdate");
			Poco::DateTime birthDate(birthDateTimestamp);
			assert(birthDate.year() == 1969 && birthDate.month() == 3 && birthDate.day() == 9);
			Poco::Timestamp lastupdatedTimestamp = doc->get<Poco::Timestamp>("lastupdated");
			assert(doc->isType<NullValue>("unknown"));
			bool active = doc->get<bool>("active");
			assert(!active);

			std::string id = doc->get("_id")->toString();
			std::cout << id << std::endl;
		}
		catch(Poco::NotFoundException& nfe)
		{
			fail(nfe.message() + " not found.");
		}
	}
	else
	{
		fail("No document returned");
	}
}
Пример #10
0
void MongoDBTest::testCursorRequest()
{
	if (!_connected)
	{
		std::cout << "Not connected, test skipped." << std::endl;
		return;
	}

	Poco::MongoDB::Database db("team");
	Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertRequest = db.createInsertRequest("numbers");
	for(int i = 0; i < 10000; ++i)
	{
		Document::Ptr doc = new Document();
		doc->add("number", i);
		insertRequest->documents().push_back(doc);
	}
	_mongo.sendRequest(*insertRequest);

	double count = db.count(_mongo, "numbers");
	assert(count == 10000);

	Poco::MongoDB::Cursor cursor("team", "numbers");

	int n = 0;
	Poco::MongoDB::ResponseMessage& response = cursor.next(_mongo);
	while(1)
	{
		n += response.documents().size();
		if ( response.cursorID() == 0 )
			break;
		response = cursor.next(_mongo);
	}
	std::cout << "n= " << n << std::endl;
	assert(n == 10000);

	Poco::MongoDB::QueryRequest drop("team.$cmd");
	drop.setNumberToReturn(1);
	drop.selector().add("drop", std::string("numbers"));

	Poco::MongoDB::ResponseMessage responseDrop;
	_mongo.sendRequest(drop, responseDrop);

	if ( responseDrop.documents().size() > 0 )
	{
		std::cout << responseDrop.documents()[0]->toString(2) << std::endl;
	}
}
Пример #11
0
Document::Ptr Database::getLastErrorDoc(Connection& connection) const
{
	Document::Ptr errorDoc;

	Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = createQueryRequest("$cmd");
	request->setNumberToReturn(1);
	request->selector().add("getLastError", 1);

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

	if ( response.documents().size() > 0 )
	{
		errorDoc = response.documents()[0];
	}

	return errorDoc;
}
Пример #12
0
void MongoDBTest::testDBCountCommand()
{
	Poco::MongoDB::Database db("team");
	Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = db.createCountRequest("players");

	Poco::MongoDB::ResponseMessage response;
	_mongo->sendRequest(*request, response);

	if ( response.documents().size() > 0 )
	{
		Poco::MongoDB::Document::Ptr doc = response.documents()[0];
		assert(doc->getInteger("n") == 1);
	}
	else
	{
		fail("Didn't get a response from the count command");
	}
}
Пример #13
0
void MongoDBTest::testCommand() {
	Poco::MongoDB::Database db("team");
	Poco::SharedPtr<Poco::MongoDB::QueryRequest> command = db.createCommand();
	command->selector().add("create", "fixCol")
		.add("capped", true)
		.add("max", 1024*1024)
		.add("size", 1024);

	Poco::MongoDB::ResponseMessage response;
	_mongo->sendRequest(*command, response);
	if ( response.documents().size() > 0 )
	{
		Poco::MongoDB::Document::Ptr doc = response.documents()[0];
	}
	else
	{
		Poco::MongoDB::Document::Ptr lastError = db.getLastErrorDoc(*_mongo);
		fail(lastError->toString(2));
	}
}
Пример #14
0
void MongoDBTest::testCountCommand()
{
	Poco::MongoDB::QueryRequest request("team.$cmd");
	request.setNumberToReturn(1);
	request.selector().add("count", std::string("players"));

	Poco::MongoDB::ResponseMessage response;

	_mongo->sendRequest(request, response);

	if ( response.documents().size() > 0 )
	{
		Poco::MongoDB::Document::Ptr doc = response.documents()[0];
		assert(doc->getInteger("n") == 1);
	}
	else
	{
		fail("Didn't get a response from the count command");
	}
}
Пример #15
0
void MongoDBTest::testDBQueryRequest()
{
	Database db("team");
	Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = db.createQueryRequest("players");
	request->selector().add("lastname" , std::string("Braem"));

	Poco::MongoDB::ResponseMessage response;
	_mongo->sendRequest(*request, response);

	if ( response.documents().size() > 0 )
	{
		Poco::MongoDB::Document::Ptr doc = response.documents()[0];

		try
		{
			std::string lastname = doc->get<std::string>("lastname");
			assert(lastname.compare("Braem") == 0);
			std::string firstname = doc->get<std::string>("firstname");
			assert(firstname.compare("Franky") == 0);
			Poco::Timestamp birthDateTimestamp = doc->get<Poco::Timestamp>("birthdate");
			Poco::DateTime birthDate(birthDateTimestamp);
			assert(birthDate.year() == 1969 && birthDate.month() == 3 && birthDate.day() == 9);
			Poco::Timestamp lastupdatedTimestamp = doc->get<Poco::Timestamp>("lastupdated");
			assert(doc->isType<NullValue>("unknown"));

			std::string id = doc->get("_id")->toString();
		}
		catch(Poco::NotFoundException& nfe)
		{
			fail(nfe.message() + " not found.");
		}
	}
	else
	{
		fail("No document returned");
	}
}
Пример #16
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;
	}
}
Пример #17
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;
	}
}
/// See https://pocoproject.org/forum/viewtopic.php?f=10&t=6326&p=11426&hilit=mongodb+auth#p11485
static void authenticate(Poco::MongoDB::Connection & connection,
	const std::string & database, const std::string & user, const std::string & password)
{
	Poco::MongoDB::Database db(database);

	/// Challenge-response authentication.
	std::string nonce;

	/// First step: request nonce.
	{
		auto command = db.createCommand();
		command->setNumberToReturn(1);
		command->selector().add<Int32>("getnonce", 1);

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

		if (response.documents().empty())
			throw Exception("Cannot authenticate in MongoDB: server returned empty response for 'getnonce' command",
				ErrorCodes::MONGODB_CANNOT_AUTHENTICATE);

		auto doc = response.documents()[0];
		try
		{
			double ok = doc->get<double>("ok", 0);
			if (ok != 1)
				throw Exception("Cannot authenticate in MongoDB: server returned response for 'getnonce' command that"
					" has field 'ok' missing or having wrong value", ErrorCodes::MONGODB_CANNOT_AUTHENTICATE);

			nonce = doc->get<std::string>("nonce", "");
			if (nonce.empty())
				throw Exception("Cannot authenticate in MongoDB: server returned response for 'getnonce' command that"
					" has field 'nonce' missing or empty", ErrorCodes::MONGODB_CANNOT_AUTHENTICATE);
		}
		catch (Poco::NotFoundException & e)
		{
			throw Exception("Cannot authenticate in MongoDB: server returned response for 'getnonce' command that has missing required field: "
				+ e.displayText(), ErrorCodes::MONGODB_CANNOT_AUTHENTICATE);
		}
	}

	/// Second step: use nonce to calculate digest and send it back to the server.
	/// Digest is hex_md5(n.nonce + username + hex_md5(username + ":mongo:" + password))
	{
		std::string first = user + ":mongo:" + password;

		Poco::MD5Engine md5;
		md5.update(first);
		std::string digest_first(Poco::DigestEngine::digestToHex(md5.digest()));
		std::string second    =  nonce + user + digest_first;
		md5.reset();
		md5.update(second);
		std::string digest_second(Poco::DigestEngine::digestToHex(md5.digest()));

		auto command = db.createCommand();
		command->setNumberToReturn(1);
		command->selector()
			.add<Int32>("authenticate", 1)
			.add<std::string>("user", user)
			.add<std::string>("nonce", nonce)
			.add<std::string>("key", digest_second);

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

		if (response.empty())
			throw Exception("Cannot authenticate in MongoDB: server returned empty response for 'authenticate' command",
				ErrorCodes::MONGODB_CANNOT_AUTHENTICATE);

		auto doc = response.documents()[0];
		try
		{
			double ok = doc->get<double>("ok", 0);
			if (ok != 1)
				throw Exception("Cannot authenticate in MongoDB: server returned response for 'authenticate' command that"
					" has field 'ok' missing or having wrong value", ErrorCodes::MONGODB_CANNOT_AUTHENTICATE);
		}
		catch (Poco::NotFoundException & e)
		{
			throw Exception("Cannot authenticate in MongoDB: server returned response for 'authenticate' command that has missing required field: "
				+ e.displayText(), ErrorCodes::MONGODB_CANNOT_AUTHENTICATE);
		}
	}
}