Esercizi Connessione MongoDB C++
Codegrind Team•Jul 12 2024
Ecco degli esercizi semplici con soluzione per praticare la connessione a un database MongoDB in C++ utilizzando diverse librerie.
Esercizio 1: Connessione a MongoDB usando la libreria mongocxx (metodo sequenziale)
Stabilire una connessione a MongoDB e stampare il nome del database.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <iostream>
int main() {
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{"mongodb://localhost:27017"}};
auto db = conn["test_db"];
std::cout << "Connesso al database: " << db.name() << std::endl;
return 0;
}
Esercizio 2: Connessione a MongoDB usando la libreria mongocxx (OOP)
Creare una classe per gestire la connessione a MongoDB e stampare il nome del database.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <iostream>
class MongoDBClient {
private:
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{"mongodb://localhost:27017"}};
public:
MongoDBClient() = default;
void stampaNomeDatabase(const std::string& db_name) {
auto db = conn[db_name];
std::cout << "Connesso al database: " << db.name() << std::endl;
}
};
int main() {
MongoDBClient client;
client.stampaNomeDatabase("test_db");
return 0;
}
Esercizio 3: Connessione a MongoDB usando la libreria mongo-c-driver (metodo sequenziale)
Stabilire una connessione a MongoDB e stampare il nome del database usando mongo-c-driver.
#include <mongoc/mongoc.h>
#include <iostream>
int main() {
mongoc_init();
mongoc_client_t *client = mongoc_client_new("mongodb://localhost:27017");
mongoc_database_t *database = mongoc_client_get_database(client, "test_db");
std::cout << "Connesso al database: " << mongoc_database_get_name(database) << std::endl;
mongoc_database_destroy(database);
mongoc_client_destroy(client);
mongoc_cleanup();
return 0;
}
Esercizio 4: Connessione a MongoDB usando la libreria mongo-c-driver (OOP)
Creare una classe per gestire la connessione a MongoDB e stampare il nome del database usando mongo-c-driver.
#include <mongoc/mongoc.h>
#include <iostream>
class MongoDBClient {
private:
mongoc_client_t *client;
public:
MongoDBClient(const std::string& uri) {
mongoc_init();
client = mongoc_client_new(uri.c_str());
}
~MongoDBClient() {
mongoc_client_destroy(client);
mongoc_cleanup();
}
void stampaNomeDatabase(const std::string& db_name) {
mongoc_database_t *database = mongoc_client_get_database(client, db_name.c_str());
std::cout << "Connesso al database: " << mongoc_database_get_name(database) << std::endl;
mongoc_database_destroy(database);
}
};
int main() {
MongoDBClient client("mongodb://localhost:27017");
client.stampaNomeDatabase("test_db");
return 0;
}
Esercizio 5: Connessione a MongoDB usando la libreria poco-mongo (metodo sequenziale)
Stabilire una connessione a MongoDB e stampare il nome del database usando poco-mongo.
#include <Poco/MongoDB/Client.h>
#include <iostream>
int main() {
Poco::MongoDB::Client client("localhost", 27017);
auto db = client.connect("test_db");
std::cout << "Connesso al database: test_db" << std::endl;
return 0;
}
Esercizio 6: Connessione a MongoDB usando la libreria poco-mongo (OOP)
Creare una classe per gestire la connessione a MongoDB e stampare il nome del database usando poco-mongo.
#include <Poco/MongoDB/Client.h>
#include <iostream>
class MongoDBClient {
private:
Poco::MongoDB::Client client;
public:
MongoDBClient(const std::string& host, int port) : client(host, port) {}
void stampaNomeDatabase(const std::string& db_name) {
auto db = client.connect(db_name);
std::cout << "Connesso al database: " << db_name << std::endl;
}
};
int main() {
MongoDBClient client("localhost", 27017);
client.stampaNomeDatabase("test_db");
return 0;
}