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"); } }
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"); } }
//DELETE players WHERE firstname = 'Victor' void sample13(Poco::MongoDB::Connection& connection) { std::cout << "*** SAMPLE 13 ***" << std::endl; Poco::MongoDB::Database db("sample"); Poco::SharedPtr<Poco::MongoDB::DeleteRequest> request = db.createDeleteRequest("players"); request->selector().add("firstname", "Victor"); connection.sendRequest(*request); Poco::MongoDB::Document::Ptr lastError = db.getLastErrorDoc(connection); std::cout << "LastError: " << lastError->toString(2) << std::endl; }
void MongoDBTest::testInsertRequest() { if (!_connected) { std::cout << "Not connected, test skipped." << std::endl; return; } Poco::MongoDB::Document::Ptr player = new Poco::MongoDB::Document(); player->add("lastname", std::string("Braem")); player->add("firstname", std::string("Franky")); Poco::DateTime birthdate; birthdate.assign(1969, 3, 9); player->add("birthdate", birthdate.timestamp()); player->add("start", 1993); player->add("active", false); Poco::DateTime now; std::cout << now.day() << " " << now.hour() << ":" << now.minute() << ":" << now.second() << std::endl; player->add("lastupdated", now.timestamp()); player->add("unknown", NullValue()); Poco::MongoDB::InsertRequest request("team.players"); request.documents().push_back(player); _mongo.sendRequest(request); }
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); }
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"); } }
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"); } }
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)); } }
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"); } }
// CREATE INDEX playername // ON players(lastname) void sample7(Poco::MongoDB::Connection& connection) { std::cout << "*** SAMPLE 7 ***" << std::endl; Poco::MongoDB::Database db("sample"); Poco::MongoDB::Document::Ptr keys = new Poco::MongoDB::Document(); keys->add("lastname", 1); Poco::MongoDB::Document::Ptr errorDoc = db.ensureIndex(connection, "players", "lastname", keys); /* Sample above is the same as the following code: Poco::MongoDB::Document::Ptr index = new Poco::MongoDB::Document(); index->add("ns", "sample.players"); index->add("name", "lastname"); index->addNewDocument("key").add("lastname", 1); Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertRequest = db.createInsertRequest("system.indexes"); insertRequest->documents().push_back(index); connection.sendRequest(*insertRequest); Poco::MongoDB::Document::Ptr errorDoc = db.getLastErrorDoc(connection); */ std::cout << errorDoc->toString(2); }
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"); } }
void MongoDBTest::testInsertRequest() { Poco::MongoDB::Document::Ptr player = new Poco::MongoDB::Document(); player->add("lastname", std::string("Braem")); player->add("firstname", std::string("Franky")); Poco::DateTime birthdate; birthdate.assign(1969, 3, 9); player->add("birthdate", birthdate.timestamp()); player->add("start", 1993); player->add("active", false); Poco::DateTime now; player->add("lastupdated", now.timestamp()); player->add("unknown", NullValue()); Poco::MongoDB::InsertRequest request("team.players"); request.documents().push_back(player); _mongo->sendRequest(request); }
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); }