Exemplo n.º 1
0
void MongoDBTest::testCursorRequest()
{
	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);

	Poco::Int64 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);
	}
	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);
}
Exemplo n.º 2
0
Poco::MongoDB::Document::Ptr Database::ensureIndex(Connection& connection, const std::string& collection, const std::string& indexName, Poco::MongoDB::Document::Ptr keys, bool unique, bool background, int version, int ttl)
{
	Poco::MongoDB::Document::Ptr index = new Poco::MongoDB::Document();
	index->add("ns", _dbname + ".players");
	index->add("name", indexName);
	index->add("key", keys);

	if ( version > 0 )
	{
		index->add("version", version);
	}

	if ( unique )
	{
		index->add("unique", true);
	}

	if ( background )
	{
		index->add("background", true);
	}

	if ( ttl > 0 )
	{
		index->add("expireAfterSeconds", ttl);
	}

	Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertRequest = createInsertRequest("system.indexes");
	insertRequest->documents().push_back(index);
	connection.sendRequest(*insertRequest);

	insertRequest->documents().push_back(index);
	connection.sendRequest(*insertRequest);

	return getLastErrorDoc(connection);
}
Exemplo n.º 3
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;
	}
}
Exemplo n.º 4
0
// INSERT INTO players
// VALUES( "Messi", "Lionel", 1987)
void sample1(Poco::MongoDB::Connection& connection)
{
	std::cout << "*** SAMPLE 1 ***" << std::endl;

	Poco::MongoDB::Database db("sample");
	Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertPlayerRequest = db.createInsertRequest("players");

	// With one insert request, we can add multiple documents
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Valdes")
		.add("firstname", "Victor")
		.add("birthyear", 1982);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Alves")
		.add("firstname", "Daniel")
		.add("birthyear", 1983);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Bartra")
		.add("firstname", "Marc")
		.add("birthyear", 1991);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Alba")
		.add("firstname", "Jordi")
		.add("birthyear", 1989);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Montoya")
		.add("firstname", "Martin")
		.add("birthyear", 1991);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Abidal")
		.add("firstname", "Eric")
		.add("birthyear", 1979);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Fontas")
		.add("firstname", "Andreu")
		.add("birthyear", 1989);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Messi")
		.add("firstname", "Lionel")
		.add("birthyear", 1987);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Puyol")
		.add("firstname", "Carles")
		.add("birthyear", 1978);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Piqué")
		.add("firstname", "Gerard")
		.add("birthyear", 1987);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Muniesa")
		.add("firstname", "Marc")
		.add("birthyear", 1992);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Fabrégas")
		.add("firstname", "Cesc")
		.add("birthyear", 1987);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Hernandez")
		.add("firstname", "Xavi")
		.add("birthyear", 1980);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Iniesta")
		.add("firstname", "Andres")
		.add("birthyear", 1984);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Alcantara")
		.add("firstname", "Thiago")
		.add("birthyear", 1991);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Dos Santos")
		.add("firstname", "Jonathan")
		.add("birthyear", 1990);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Mascherano")
		.add("firstname", "Javier")
		.add("birthyear", 1984);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Busquets")
		.add("firstname", "Sergio")
		.add("birthyear", 1988);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Adriano")
		.add("firstname", "")
		.add("birthyear", 1984);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Song")
		.add("firstname", "Alex")
		.add("birthyear", 1987);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Villa")
		.add("firstname", "David")
		.add("birthyear", 1981);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Sanchez")
		.add("firstname", "Alexis")
		.add("birthyear", 1988);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Pedro")
		.add("firstname", "")
		.add("birthyear", 1987);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Cuenca")
		.add("firstname", "Isaac")
		.add("birthyear", 1991);
	insertPlayerRequest->addNewDocument()
		.add("lastname", "Tello")
		.add("firstname", "Cristian")
		.add("birthyear", 1991);

	std::cout << insertPlayerRequest->documents().size() << std::endl;

	connection.sendRequest(*insertPlayerRequest);
	std::string lastError = db.getLastError(connection);
	if ( !lastError.empty() )
	{
		std::cout << "Last Error: " << db.getLastError(connection) << std::endl;
	}
}