Esercizi Inserimento MongoDB C++
Codegrind Team•Jul 12 2024
Ecco degli esercizi semplici con soluzione per praticare l’inserimento singolo e multiplo di documenti in MongoDB utilizzando C++ sia in modo sequenziale che OOP.
Esercizio 1: Inserimento singolo di un documento usando mongocxx (metodo sequenziale)
Stabilire una connessione a MongoDB ed inserire 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 document{};
document.append(bsoncxx::builder::basic::kvp("name", "John Doe"));
document.append(bsoncxx::builder::basic::kvp("age", 30));
auto result = collection.insert_one(document.view());
if (result) {
std::cout << "Documento inserito con ID: " << result->inserted_id().get_oid().value.to_string() << std::endl;
}
return 0;
}
Esercizio 2: Inserimento multiplo di documenti usando mongocxx (metodo sequenziale)
Stabilire una connessione a MongoDB ed inserire 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>
#include <vector>
int main() {
mongocxx::instance instance{};
mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017"}};
auto collection = client["test_db"]["test_collection"];
bsoncxx::builder::basic::document document1{};
document1.append(bsoncxx::builder::basic::kvp("name", "Alice"));
document1.append(bsoncxx::builder::basic::kvp("age", 25));
bsoncxx::builder::basic::document document2{};
document2.append(bsoncxx::builder::basic::kvp("name", "Bob"));
document2.append(bsoncxx::builder::basic::kvp("age", 28));
std::vector<bsoncxx::document::value> documents;
documents.push_back(document1.extract());
documents.push_back(document2.extract());
auto result = collection.insert_many(documents);
if (result) {
std::cout << "Documenti inseriti: " << result->inserted_count() << std::endl;
}
return 0;
}
Esercizio 3: Inserimento singolo di un documento usando mongocxx (OOP)
Creare una classe per gestire la connessione a MongoDB ed inserire 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 inserisciDocumento(const std::string& db_name, const std::string& coll_name, const bsoncxx::document::view& document) {
auto collection = client[db_name][coll_name];
auto result = collection.insert_one(document);
if (result) {
std::cout << "Documento inserito con ID: " << result->inserted_id().get_oid().value.to_string() << std::endl;
}
}
};
int main() {
MongoDBClient client;
bsoncxx::builder::basic::document document{};
document.append(bsoncxx::builder::basic::kvp("name", "John Doe"));
document.append(bsoncxx::builder::basic::kvp("age", 30));
client.inserisciDocumento("test_db", "test_collection", document.view());
return 0;
}
Esercizio 4: Inserimento multiplo di documenti usando mongocxx (OOP)
Creare una classe per gestire la connessione a MongoDB ed inserire 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>
#include <vector>
class MongoDBClient {
private:
mongocxx::instance instance{};
mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017"}};
public:
MongoDBClient() = default;
void inserisciDocumenti(const std::string& db_name, const std::string& coll_name, const std::vector<bsoncxx::document::value>& documents) {
auto collection = client[db_name][coll_name];
auto result = collection.insert_many(documents);
if (result) {
std::cout << "Documenti inseriti: " << result->inserted_count() << std::endl;
}
}
};
int main() {
MongoDBClient client;
bsoncxx::builder::basic::document document1{};
document1.append(bsoncxx::builder::basic::kvp("name", "Alice"));
document1.append(bsoncxx::builder::basic::kvp("age", 25));
bsoncxx::builder::basic::document document2{};
document2.append(bsoncxx::builder::basic::kvp("name", "Bob"));
document2.append(bsoncxx::builder::basic::kvp("age", 28));
std::vector<bsoncxx::document::value> documents;
documents.push_back(document1.extract());
documents.push_back(document2.extract());
client.inserisciDocumenti("test_db", "test_collection", documents);
return 0;
}
Esercizio 5: Inserimento singolo di un documento usando mongo-c-driver (metodo sequenziale)
Stabilire una connessione a MongoDB ed inserire 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 *document = BCON_NEW(
"name", BCON_UTF8("John Doe"),
"age", BCON_INT32(30)
);
bson_error_t error;
if (!mongoc_collection_insert_one(collection, document, nullptr, nullptr, &error)) {
std::cerr << "Errore nell'inserimento del documento: " << error.message << std::endl;
} else {
std::cout << "Documento inserito con successo." << std::endl;
}
bson_destroy(document);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
mongoc_cleanup();
return 0;
}
Esercizio 6: Inserimento multiplo di documenti usando mongo-c-driver (metodo sequenziale)
Stabilire una connessione a MongoDB ed inserire più documenti usando mongo-c-driver.
#include <mongoc/mongoc.h>
#include <bson/bson.h>
#include <iostream>
#include <vector>
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 *document1 = BCON_NEW(
"name", BCON_UTF8("Alice"),
"age", BCON_INT32(25)
);
bson_t *document2 = BCON_NEW(
"name", BCON_UTF8("Bob"),
"age", BCON_INT32(28)
);
std::vector<bson_t*> documents = {document1, document2};
bson_error_t error;
if (!mongoc_collection_insert_many(collection, (const bson_t**)documents.data(), documents.size(), nullptr, nullptr, &error)) {
std::cerr << "Errore nell'inserimento dei documenti: " << error.message << std::endl;
} else {
std::cout << "Documenti inseriti con successo." << std::endl;
}
bson_destroy(document1);
bson_destroy(document2);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
mongoc_cleanup();
return 0;
}
Esercizio 7: Inserimento singolo di un documento usando mongo-c-driver (OOP)
Creare una classe per gestire la connessione a MongoDB ed inserire 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 inserisciDocumento(const std::string& db_name, const std::string& coll_name, const bson_t* document) {
mongoc_collection_t *collection = mongoc_client_get_collection(client, db_name.c_str(), coll_name.c_str());
bson_error_t error;
if (!mongoc_collection_insert_one(collection, document, nullptr, nullptr, &error)) {
std::cerr << "Errore nell'inserimento del documento: " << error.message << std::endl;
} else {
std::cout << "Documento inserito con successo." << std::endl;
}
mongoc_collection_destroy(collection);
}
};
int main() {
MongoDBClient client("mongodb://localhost:27017");
bson_t *document = BCON_NEW(
"name", BCON_UTF8("John Doe"),
"age", BCON_INT32(30)
);
client.inserisciDocumento("test_db", "test_collection", document);
bson_destroy(document);
return 0;
}
Esercizio 8: Inserimento multiplo di documenti usando mongo-c-driver (OOP)
Creare una classe per gestire la connessione a MongoDB ed inserire più documenti usando mongo-c-driver.
#include <mongoc/mongoc.h>
#include <bson/bson.h>
#include <iostream>
#include <vector>
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 inserisciDocumenti(const std::string& db_name, const std::string& coll_name, const std::vector<bson_t*>& documents) {
mongoc_collection_t *collection = mongoc_client_get_collection(client, db_name.c_str(), coll_name.c_str());
bson_error_t error;
if (!mongoc_collection_insert_many(collection, (const bson_t**)documents.data(), documents.size(), nullptr, nullptr, &error)) {
std::cerr << "Errore nell'inserimento dei documenti: " << error.message << std::endl;
} else {
std::cout << "Documenti inseriti con successo." << std::endl;
}
mongoc_collection_destroy(collection);
}
};
int main() {
MongoDBClient client("mongodb://localhost:27017");
bson_t *document1 = BCON_NEW(
"name", BCON_UTF8("Alice"),
"age", BCON_INT32(25)
);
bson_t *document2 = BCON_NEW(
"name", BCON_UTF8("Bob"),
"age", BCON_INT32(28)
);
std::vector<bson_t*> documents = {document1, document2};
client.inserisciDocumenti("test_db", "test_collection", documents);
bson_destroy(document1);
bson_destroy(document2);
return 0;
}