📢 Nuovo Corso Bootstrap Completo disponibile!

Esercizi Lettura MongoDB C++

Ecco degli esercizi semplici con soluzione per praticare la lettura di dati in MongoDB utilizzando C++ sia in modo sequenziale che OOP.

Esercizio 1: Lettura di un singolo documento usando mongocxx (metodo sequenziale)

Stabilire una connessione a MongoDB e leggere un singolo documento.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/builder/basic/document.hpp>
#include <iostream>
int main() {
mongocxx::instance instance{};
mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017"}};
auto collection = client["test_db"]["test_collection"];
bsoncxx::builder::basic::document filter{};
filter.append(bsoncxx::builder::basic::kvp("name", "John Doe"));
auto result = collection.find_one(filter.view());
if (result) {
std::cout << bsoncxx::to_json(*result) << std::endl;
} else {
std::cout << "Documento non trovato" << std::endl;
}
return 0;
}

Esercizio 2: Lettura di più documenti usando mongocxx (metodo sequenziale)

Stabilire una connessione a MongoDB e leggere più documenti.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/builder/basic/document.hpp>
#include <iostream>
int main() {
mongocxx::instance instance{};
mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017"}};
auto collection = client["test_db"]["test_collection"];
bsoncxx::builder::basic::document filter{};
filter.append(bsoncxx::builder::basic::kvp("status", "active"));
auto cursor = collection.find(filter.view());
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
return 0;
}

Esercizio 3: Lettura di un singolo documento usando mongocxx (OOP)

Creare una classe per gestire la connessione a MongoDB e leggere un singolo documento.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/builder/basic/document.hpp>
#include <iostream>
class MongoDBClient {
private:
mongocxx::instance instance{};
mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017"}};
public:
MongoDBClient() = default;
void leggiDocumento(const std::string& db_name, const std::string& coll_name, const bsoncxx::document::view& filter) {
auto collection = client[db_name][coll_name];
auto result = collection.find_one(filter);
if (result) {
std::cout << bsoncxx::to_json(*result) << std::endl;
} else {
std::cout << "Documento non trovato" << std::endl;
}
}
};
int main() {
MongoDBClient client;
bsoncxx::builder::basic::document filter{};
filter.append(bsoncxx::builder::basic::kvp("name", "John Doe"));
client.leggiDocumento("test_db", "test_collection", filter.view());
return 0;
}

Esercizio 4: Lettura di più documenti usando mongocxx (OOP)

Creare una classe per gestire la connessione a MongoDB e leggere più documenti.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/builder/basic/document.hpp>
#include <iostream>
class MongoDBClient {
private:
mongocxx::instance instance{};
mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017"}};
public:
MongoDBClient() = default;
void leggiDocumenti(const std::string& db_name, const std::string& coll_name, const bsoncxx::document::view& filter) {
auto collection = client[db_name][coll_name];
auto cursor = collection.find(filter);
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
};
int main() {
MongoDBClient client;
bsoncxx::builder::basic::document filter{};
filter.append(bsoncxx::builder::basic::kvp("status", "active"));
client.leggiDocumenti("test_db", "test_collection", filter.view());
return 0;
}

Esercizio 5: Lettura di un singolo documento usando mongo-c-driver (metodo sequenziale)

Stabilire una connessione a MongoDB e leggere un singolo documento usando mongo-c-driver.
#include <mongoc/mongoc.h>
#include <bson/bson.h>
#include <iostream>
int main() {
mongoc_init();
mongoc_client_t *client = mongoc_client_new("mongodb://localhost:27017");
mongoc_collection_t *collection = mongoc_client_get_collection(client, "test_db", "test_collection");
bson_t *filter = BCON_NEW("name", BCON_UTF8("John Doe"));
mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(collection, filter, nullptr, nullptr);
const bson_t *doc;
if (mongoc_cursor_next(cursor, &doc)) {
char *str = bson_as_canonical_extended_json(doc, nullptr);
std::cout << str << std::endl;
bson_free(str);
} else {
std::cout << "Documento non trovato" << std::endl;
}
bson_destroy(filter);
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
mongoc_cleanup();
return 0;
}

Esercizio 6: Lettura di più documenti usando mongo-c-driver (metodo sequenziale)

Stabilire una connessione a MongoDB e leggere più documenti usando mongo-c-driver.
#include <mongoc/mongoc.h>
#include <bson/bson.h>
#include <iostream>
int main() {
mongoc_init();
mongoc_client_t *client = mongoc_client_new("mongodb://localhost:27017");
mongoc_collection_t *collection = mongoc_client_get_collection(client, "test_db", "test_collection");
bson_t *filter = BCON_NEW("status", BCON_UTF8("active"));
mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(collection, filter, nullptr, nullptr);
const bson_t *doc;
while (mongoc_cursor_next(cursor, &doc)) {
char *str = bson_as_canonical_extended_json(doc, nullptr);
std::cout << str << std::endl;
bson_free(str);
}
bson_destroy(filter);
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
mongoc_cleanup();
return 0;
}

Esercizio 7: Lettura di un singolo documento usando mongo-c-driver (OOP)

Creare una classe per gestire la connessione a MongoDB e leggere un singolo documento usando mongo-c-driver.
#include <mongoc/mongoc.h>
#include <bson/bson.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 leggiDocumento(const std::string& db_name, const std::string& coll_name, const bson_t* filter) {
mongoc_collection_t *collection = mongoc_client_get_collection(client, db_name.c_str(), coll_name.c_str());
mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(collection, filter, nullptr, nullptr);
const bson_t *doc;
if (mongoc_cursor_next(cursor, &doc)) {
char *str = bson_as_canonical_extended_json(doc, nullptr);
std::cout << str << std::endl;
bson_free(str);
} else {
std::cout << "Documento non trovato" << std::endl;
}
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
}
};
int main() {
MongoDBClient client("mongodb://localhost:27017");
bson_t *filter = BCON_NEW("name", BCON_UTF8("John Doe"));
client.leggiDocumento("test_db", "test_collection", filter);
bson_destroy(filter);
return 0;
}

Esercizio 8: Lettura di più documenti usando mongo-c-driver (OOP)

Creare una classe per gestire la connessione a MongoDB e leggere più documenti usando mongo-c-driver.
#include <mongoc/mongoc.h>
#include <bson/bson.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 leggiDocumenti(const std::string& db_name, const std::string& coll_name, const bson_t* filter) {
mongoc_collection_t *collection = mongoc_client_get_collection(client, db_name.c_str(), coll_name.c_str());
mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(collection, filter, nullptr, nullptr);
const bson_t *doc;
while (mongoc_cursor_next(cursor, &doc)) {
char *str = bson_as_canonical_extended_json(doc, nullptr);
std::cout << str << std::endl;
bson_free(str);
}
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
}
};
int main() {
MongoDBClient client("mongodb://localhost:27017");
bson_t *filter = BCON_NEW("status", BCON_UTF8("active"));
client.leggiDocumenti("test_db", "test_collection", filter);
bson_destroy(filter);
return 0;
}