Esercizi Modifica MongoDB C++
Codegrind Team•Jul 12 2024
Ecco degli esercizi semplici con soluzione per praticare la modifica di dati in MongoDB utilizzando C++ sia in modo sequenziale che OOP.
Esercizio 1: Modifica di un singolo documento usando mongocxx (metodo sequenziale)
Stabilire una connessione a MongoDB e modificare 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"));
bsoncxx::builder::basic::document update{};
update.append(bsoncxx::builder::basic::kvp("$set", bsoncxx::builder::basic::make_document(
bsoncxx::builder::basic::kvp("age", 35)
)));
auto result = collection.update_one(filter.view(), update.view());
if (result) {
std::cout << "Documenti modificati: " << result->modified_count() << std::endl;
} else {
std::cout << "Nessun documento modificato" << std::endl;
}
return 0;
}
Esercizio 2: Modifica di più documenti usando mongocxx (metodo sequenziale)
Stabilire una connessione a MongoDB e modificare 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"));
bsoncxx::builder::basic::document update{};
update.append(bsoncxx::builder::basic::kvp("$set", bsoncxx::builder::basic::make_document(
bsoncxx::builder::basic::kvp("status", "inactive")
)));
auto result = collection.update_many(filter.view(), update.view());
if (result) {
std::cout << "Documenti modificati: " << result->modified_count() << std::endl;
} else {
std::cout << "Nessun documento modificato" << std::endl;
}
return 0;
}
Esercizio 3: Modifica di un singolo documento usando mongocxx (OOP)
Creare una classe per gestire la connessione a MongoDB e modificare 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 modificaDocumento(const std::string& db_name, const std::string& coll_name, const bsoncxx::document::view& filter, const bsoncxx::document::view& update) {
auto collection = client[db_name][coll_name];
auto result = collection.update_one(filter, update);
if (result) {
std::cout << "Documenti modificati: " << result->modified_count() << std::endl;
} else {
std::cout << "Nessun documento modificato" << std::endl;
}
}
};
int main() {
MongoDBClient client;
bsoncxx::builder::basic::document filter{};
filter.append(bsoncxx::builder::basic::kvp("name", "John Doe"));
bsoncxx::builder::basic::document update{};
update.append(bsoncxx::builder::basic::kvp("$set", bsoncxx::builder::basic::make_document(
bsoncxx::builder::basic::kvp("age", 35)
)));
client.modificaDocumento("test_db", "test_collection", filter.view(), update.view());
return 0;
}
Esercizio 4: Modifica di più documenti usando mongocxx (OOP)
Creare una classe per gestire la connessione a MongoDB e modificare 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 modificaDocumenti(const std::string& db_name, const std::string& coll_name, const bsoncxx::document::view& filter, const bsoncxx::document::view& update) {
auto collection = client[db_name][coll_name];
auto result = collection.update_many(filter, update);
if (result) {
std::cout << "Documenti modificati: " << result->modified_count() << std::endl;
} else {
std::cout << "Nessun documento modificato" << std::endl;
}
}
};
int main() {
MongoDBClient client;
bsoncxx::builder::basic::document filter{};
filter.append(bsoncxx::builder::basic::kvp("status", "active"));
bsoncxx::builder::basic::document update{};
update.append(bsoncxx::builder::basic::kvp("$set", bsoncxx::builder::basic::make_document(
bsoncxx::builder::basic::kvp("status", "inactive")
)));
client.modificaDocumenti("test_db", "test_collection", filter.view(), update.view());
return 0;
}
Esercizio 5: Modifica di un singolo documento usando mongo-c-driver (metodo sequenziale)
Stabilire una connessione a MongoDB e modificare 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"));
bson_t *update = BCON_NEW("$set", "{", "age", BCON_INT32(35), "}");
bson_error_t error;
if (!mongoc_collection_update_one(collection, filter, update, nullptr, nullptr, &error)) {
std::cerr << "Errore nella modifica del documento: " << error.message << std::endl;
} else {
std::cout << "Documento modificato con successo" << std::endl;
}
bson_destroy(filter);
bson_destroy(update);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
mongoc_cleanup();
return 0;
}
Esercizio 6: Modifica di più documenti usando mongo-c-driver (OOP)
Creare una classe per gestire la connessione a MongoDB e
modificare 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 modificaDocumenti(const std::string& db_name, const std::string& coll_name, const bson_t* filter, const bson_t* update) {
mongoc_collection_t *collection = mongoc_client_get_collection(client, db_name.c_str(), coll_name.c_str());
bson_error_t error;
if (!mongoc_collection_update_many(collection, filter, update, nullptr, nullptr, &error)) {
std::cerr << "Errore nella modifica dei documenti: " << error.message << std::endl;
} else {
std::cout << "Documenti modificati con successo" << std::endl;
}
mongoc_collection_destroy(collection);
}
};
int main() {
MongoDBClient client("mongodb://localhost:27017");
bson_t *filter = BCON_NEW("status", BCON_UTF8("active"));
bson_t *update = BCON_NEW("$set", "{", "status", BCON_UTF8("inactive"), "}");
client.modificaDocumenti("test_db", "test_collection", filter, update);
bson_destroy(filter);
bson_destroy(update);
return 0;
}