Main Content

aggregate

R2026b

Run MongoDB aggregation pipeline

Since R2026b

    Description

    data = aggregate(mongodbconn,collectname,pipeline) returns aggregated data from the specified MongoDB® collection by running an aggregation pipeline. Use aggregate when you want MongoDB to filter, group, or summarize data before returning results to MATLAB®.

    example

    Examples

    collapse all

    Create a collection of sample documents, run a multi‑stage aggregation pipeline to filter and summarize the data, and return the results.

    Use the mongoc function to create the database connection.

    port = 27017;
    dbname = "mongotest";
    
    mongodbconn = mongoc(getSecret("mongoc_server"),port,dbname, ... 
        UserName = getSecret("mongoc_username"), ... 
        Password = getSecret("mongoc_password"));

    Insert sample documents into a collection.

    collectname = "employeeCollection";
    
    docs(1) = struct("name", "Alice", "department", "Engineering", "status", "active", "salary", 90000);
    docs(2) = struct("name", "Bob",   "department", "Engineering", "status", "inactive", "salary", 85000);
    docs(3) = struct("name", "Carol", "department", "HR",          "status", "active",   "salary", 60000);
    docs(4) = struct("name", "David", "department", "HR",          "status", "active",   "salary", 62000);
    docs(5) = struct("name", "Eve",   "department", "Sales",       "status", "active",   "salary", 70000);
    docs(6) = struct("name", "Frank", "department", "Sales",       "status", "inactive", "salary", 68000);
    
    createCollection(mongodbconn,collectname);
    insert(mongodbconn,collectname,docs);

    Run a three-stage aggregation pipeline to process the data.

    pipeline = {
        '{ "$match": { "status": "active" } }'
        '{ "$group": { "_id": "$department", "totalSalary": { "$sum": "$salary" } } }'
        '{ "$sort": { "totalSalary": -1 } }'
        };
    
    data = aggregate(mongodbconn,collectname,pipeline)
    data = 3×1 struct array with fields:
        _id
        totalSalary
    
    

    Close the database connection.

    dropCollection(mongodbconn,collectname);
    close(mongodbconn);

    Input Arguments

    collapse all

    MongoDB C++ interface connection, specified as a connection object.

    MongoDB collection name, specified as a string scalar. The aggregation pipeline runs on documents in this collection.

    Aggregation pipeline, specified as a string scalar, string array, or cell array of character vectors. This input defines an ordered sequence of MongoDB aggregation stages, expressed using JSON-style syntax, that transform documents in the collection. Each stage performs a specific operation and passes its output to the next stage.

    For example, { "$match": { "status": "active" } } creates a single-stage pipeline that filters documents based on the specified condition. Pipelines often include multiple stages, such as $match, $group, and $sort.

    Example: {'{ "$sort": { "totalSalary": -1 } }'}

    Output Arguments

    collapse all

    Aggregated data from the specified collection, represented as MATLAB structures:

    • Structure for a single document

    • Structure array for multiple documents with the same fields

    • Cell array of structures for documents with different fields

    Each structure represents a JSON-style document returned by the aggregation pipeline.

    Version History

    Introduced in R2026b