🚀 Nuova versione beta disponibile! Feedback o problemi? Contattaci

Esercizi Aggregazioni MongoDB C++

Codegrind Team•Jul 12 2024

Ecco degli esercizi con soluzione per praticare le basi delle aggregazioni in MongoDB utilizzando C++.

Esercizio 1

Collegarsi a un database MongoDB e contare il numero di documenti in una collezione.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <iostream>

int main() {
    mongocxx::instance instance{};
    mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017"}};

    auto db = client["test_db"];
    auto collection = db["test_collection"];
    auto count = collection.count_documents({});

    std::cout << "Numero di documenti: " << count << std::endl;
    return 0;
}

Esercizio 2

Utilizzare l'aggregazione per contare il numero di documenti in base a un campo specifico.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/pipeline.hpp>
#include <iostream>

int main() {
    mongocxx::instance instance{};
    mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017"}};

    auto db = client["test_db"];
    auto collection = db["test_collection"];

    mongocxx::pipeline p{};
    p.group(mongocxx::document{} << "_id" << "$campo" << "count" << mongocxx::make_document(mongocxx::kvp("$sum", 1)) << mongocxx::finalize);

    auto cursor = collection.aggregate(p);
    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }

    return 0;
}

Esercizio 3

Utilizzare l'aggregazione per calcolare la somma di un campo numerico.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/pipeline.hpp>
#include <iostream>

int main() {
    mongocxx::instance instance{};
    mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017"}};

    auto db = client["test_db"];
    auto collection = db["test_collection"];

    mongocxx::pipeline p{};
    p.group(mongocxx::document{} << "_id" << bsoncxx::types::b_null{} << "total" << mongocxx::make_document(mongocxx::kvp("$sum", "$campo_numerico")) << mongocxx::finalize);

    auto cursor = collection.aggregate(p);
    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }

    return 0;
}

Esercizio 4

Utilizzare l'aggregazione per calcolare la media di un campo numerico.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/pipeline.hpp>
#include <iostream>

int main() {
    mongocxx::instance instance{};
    mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017"}};

    auto db = client["test_db"];
    auto collection = db["test_collection"];

    mongocxx::pipeline p{};
    p.group(mongocxx::document{} << "_id" << bsoncxx::types::b_null{} << "average" << mongocxx::make_document(mongocxx::kvp("$avg", "$campo_numerico")) << mongocxx::finalize);

    auto cursor = collection.aggregate(p);
    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }

    return 0;
}

Esercizio 5

Utilizzare l'aggregazione per trovare il valore massimo di un campo numerico.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/pipeline.hpp>
#include <iostream>

int main() {
    mongocxx::instance instance{};
    mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017"}};

    auto db = client["test_db"];
    auto collection = db["test_collection"];

    mongocxx::pipeline p{};
    p.group(mongocxx::document{} << "_id" << bsoncxx::types::b_null{} << "max_value" << mongocxx::make_document(mongocxx::kvp("$max", "$campo_numerico")) << mongocxx::finalize);

    auto cursor = collection.aggregate(p);
    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }

    return 0;
}

Esercizio 6

Utilizzare l'aggregazione per trovare il valore minimo di un campo numerico.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/pipeline.hpp>
#include <iostream>

int main() {
    mongocxx::instance instance{};
    mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017"}};

    auto db = client["test_db"];
    auto collection = db["test_collection"];

    mongocxx::pipeline p{};
    p.group(mongocxx::document{} << "_id" << bsoncxx::types::b_null{} << "min_value" << mongocxx::make_document(mongocxx::kvp("$min", "$campo_numerico")) << mongocxx::finalize);

    auto cursor = collection.aggregate(p);
    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }

    return 0;
}

Esercizio 7

Utilizzare l'aggregazione per ordinare i documenti in base a un campo specifico.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/pipeline.hpp>
#include <iostream>

int main() {
    mongocxx::instance instance{};
    mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017"}};

    auto db = client["test_db"];
    auto collection = db["test_collection"];

    mongocxx::pipeline p{};
    p.sort(mongocxx::document{} << "campo" << 1 << mongocxx::finalize);

    auto cursor = collection.aggregate(p);
    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }

    return 0;
}

Esercizio 8

Utilizzare l'aggregazione per filtrare i documenti in base a un criterio specifico.
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/pipeline.hpp>
#include <iostream>

int main() {
    mongocxx::instance instance{};
    mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017"}};

    auto db = client["test_db"];
    auto collection = db["test_collection"];

    mongocxx::pipeline p{};
    p.match(mongocxx::document{} << "campo" << bson

cxx::types::bson_value::view(bsoncxx::types::b_int32{10}) << mongocxx::finalize);

    auto cursor = collection.aggregate(p);
    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }

    return 0;
}