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::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); }
// 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::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); }