Esercizi Eliminazione MongoDB C++
Codegrind Team•Jul 12 2024
Ecco degli esercizi semplici con soluzione per praticare l’eliminazione singola e multipla di documenti in MongoDB utilizzando C++ sia in modo sequenziale che OOP.
Esercizio 1: Eliminazione singola di un documento usando mongocxx (metodo sequenziale)
Stabilire una connessione a MongoDB ed eliminare un singolo documento.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/stdx.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.delete_one(filter.view());
if (result) {
std::cout << "Eliminati " << result->deleted_count() << " documenti." << std::endl;
} else {
std::cout << "Nessun documento eliminato." << std::endl;
}
return 0;
}
Esercizio 2: Eliminazione multipla di documenti usando mongocxx (metodo sequenziale)
Stabilire una connessione a MongoDB ed eliminare più documenti.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/stdx.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", "inactive"));
auto result = collection.delete_many(filter.view());
if (result) {
std::cout << "Eliminati " << result->deleted_count() << " documenti." << std::endl;
} else {
std::cout << "Nessun documento eliminato." << std::endl;
}
return 0;
}
Esercizio 3: Eliminazione singola di un documento usando mongocxx (OOP)
Creare una classe per gestire la connessione a MongoDB ed eliminare un singolo documento.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/stdx.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 eliminaDocumentoSingolo(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.delete_one(filter);
if (result) {
std::cout << "Eliminati " << result->deleted_count() << " documenti." << std::endl;
} else {
std::cout << "Nessun documento eliminato." << std::endl;
}
}
};
int main() {
MongoDBClient client;
bsoncxx::builder::basic::document filter;
filter.append(bsoncxx::builder::basic::kvp("name", "John Doe"));
client.eliminaDocumentoSingolo("test_db", "test_collection", filter.view());
return 0;
}
Esercizio 4: Eliminazione multipla di documenti usando mongocxx (OOP)
Creare una classe per gestire la connessione a MongoDB ed eliminare più documenti.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/stdx.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 eliminaDocumentiMultipli(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.delete_many(filter);
if (result) {
std::cout << "Eliminati " << result->deleted_count() << " documenti." << std::endl;
} else {
std::cout << "Nessun documento eliminato." << std::endl;
}
}
};
int main() {
MongoDBClient client;
bsoncxx::builder::basic::document filter;
filter.append(bsoncxx::builder::basic::kvp("status", "inactive"));
client.eliminaDocumentiMultipli("test_db", "test_collection", filter.view());
return 0;
}
Esercizio 5: Eliminazione singola di un documento usando mongo-c-driver (metodo sequenziale)
Stabilire una connessione a MongoDB ed eliminare 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_error_t error;
if (mongoc_collection_delete_one(collection, filter, nullptr, nullptr, &error)) {
std::cout << "Documento eliminato con successo." << std::endl;
} else {
std::cerr << "Errore nell'eliminazione del documento: " << error.message << std::endl;
}
bson_destroy(filter);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
mongoc_cleanup();
return 0;
}
Esercizio 6: Eliminazione multipla di documenti usando mongo-c-driver (metodo sequenziale)
Stabilire una connessione a MongoDB ed eliminare 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("inactive"));
bson_error_t error;
if (mongoc_collection_delete_many(collection, filter, nullptr, nullptr, &error)) {
std::cout << "Documenti eliminati con successo." << std::endl;
} else {
std::cerr << "Errore nell'eliminazione dei documenti: " << error.message << std::endl;
}
bson_destroy(filter);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
mongoc_cleanup();
return 0;
}
Esercizio 7: Eliminazione singola di un documento usando mongo-c-driver (OOP)
Creare una classe per gestire
la connessione a MongoDB ed eliminare 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 eliminaDocumentoSingolo(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());
bson_error_t error;
if (mongoc_collection_delete_one(collection, filter, nullptr, nullptr, &error)) {
std::cout << "Documento eliminato con successo." << std::endl;
} else {
std::cerr << "Errore nell'eliminazione del documento: " << error.message << std::endl;
}
mongoc_collection_destroy(collection);
}
};
int main() {
MongoDBClient client("mongodb://localhost:27017");
bson_t *filter = BCON_NEW("name", BCON_UTF8("John Doe"));
client.eliminaDocumentoSingolo("test_db", "test_collection", filter);
bson_destroy(filter);
return 0;
}
Esercizio 8: Eliminazione multipla di documenti usando mongo-c-driver (OOP)
Creare una classe per gestire la connessione a MongoDB ed eliminare 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 eliminaDocumentiMultipli(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());
bson_error_t error;
if (mongoc_collection_delete_many(collection, filter, nullptr, nullptr, &error)) {
std::cout << "Documenti eliminati con successo." << std::endl;
} else {
std::cerr << "Errore nell'eliminazione dei documenti: " << error.message << std::endl;
}
mongoc_collection_destroy(collection);
}
};
int main() {
MongoDBClient client("mongodb://localhost:27017");
bson_t *filter = BCON_NEW("status", BCON_UTF8("inactive"));
client.eliminaDocumentiMultipli("test_db", "test_collection", filter);
bson_destroy(filter);
return 0;
}
Esercizio 9: Eliminazione singola di un documento usando Poco MongoDB (metodo sequenziale)
Stabilire una connessione a MongoDB ed eliminare un singolo documento usando Poco MongoDB.
#include <Poco/MongoDB/Client.h>
#include <Poco/MongoDB/Database.h>
#include <Poco/MongoDB/Collection.h>
#include <Poco/MongoDB/QueryRequest.h>
#include <iostream>
int main() {
Poco::MongoDB::Client client("localhost", 27017);
Poco::MongoDB::Database db("test_db");
Poco::MongoDB::Collection collection(db, "test_collection");
Poco::MongoDB::QueryRequest::Ptr query(new Poco::MongoDB::QueryRequest("test_db.test_collection"));
query->selector().add("name", "John Doe");
try {
collection.remove(*query);
std::cout << "Documento eliminato con successo." << std::endl;
} catch (const Poco::Exception& e) {
std::cerr << "Errore: " << e.displayText() << std::endl;
}
return 0;
}
Esercizio 10: Eliminazione multipla di documenti usando Poco MongoDB (OOP)
Creare una classe per gestire la connessione a MongoDB ed eliminare più documenti usando Poco MongoDB.
#include <Poco/MongoDB/Client.h>
#include <Poco/MongoDB/Database.h>
#include <Poco/MongoDB/Collection.h>
#include <Poco/MongoDB/QueryRequest.h>
#include <iostream>
class MongoDBClient {
private:
Poco::MongoDB::Client client;
public:
MongoDBClient(const std::string& host, int port) : client(host, port) {}
void eliminaDocumentiMultipli(const std::string& db_name, const std::string& coll_name, const Poco::MongoDB::Document::Ptr& filter) {
Poco::MongoDB::Database db(db_name);
Poco::MongoDB::Collection collection(db, coll_name);
Poco::MongoDB::QueryRequest::Ptr query(new Poco::MongoDB::QueryRequest(db_name + "." + coll_name));
query->selector().add(filter);
try {
collection.remove(*query);
std::cout << "Documenti eliminati con successo." << std::endl;
} catch (const Poco::Exception& e) {
std::cerr << "Errore: " << e.displayText() << std::endl;
}
}
};
int main() {
MongoDBClient client("localhost", 27017);
Poco::MongoDB::Document::Ptr filter(new Poco::MongoDB::Document);
filter->add("status", "inactive");
client.eliminaDocumentiMultipli("test_db", "test_collection", filter);
return 0;
}