Contenuto principale

distinct

R2026b

Retrieve distinct values from MongoDB collection

Since R2026b

    Description

    values = distinct(mongodbconn,collectname,field) returns the unique values of the specified field from all documents in the MongoDB® collection.

    values = distinct(mongodbconn,collectname,field,Query=mongoquery) specifies additional options using one or more name-value arguments. For example, specify a query string using mongoquery to filter documents before retrieving distinct values. Use this syntax to retrieve distinct values from a subset of documents that match specific criteria.

    example

    Examples

    collapse all

    Retrieve distinct values from all documents in a MongoDB® collection and from a subset that matches a query.

    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"));

    Create a set of documents and insert them into a collection.

    collection = "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,collection);
    insert(mongodbconn,collection,docs);

    Retrieve distinct values from all documents in the collection.

    field = "name";
    values = distinct(mongodbconn,collection,field)
    values = 6×1 cell array
        {'Alice'}
        {'Bob'  }
        {'Carol'}
        {'David'}
        {'Eve'  }
        {'Frank'}
    
    

    Filter documents using a query and retrieve distinct values.

    mongoquery = "{""department"":""Sales""}";
    values = distinct(mongodbconn,collection,field,Query=mongoquery)
    values = 2×1 cell array
        {'Eve'  }
        {'Frank'}
    
    

    Close the database connection.

    dropCollection(mongodbconn,collection);
    close(mongodbconn);

    Input Arguments

    collapse all

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

    MongoDB collection name, specified as a string scalar or character vector. The function retrieves distinct values from documents in the specified collection.

    Field name in the MongoDB collection, specified as a string scalar or character vector. The function retrieves distinct values from the specified field in documents in the collection.

    MongoDB query, specified as a string scalar or character vector. The query filters documents in the collection before retrieving distinct values from the specified field. Specify the query using JSON-style syntax.

    Example: "{""department_id"":80}"

    Output Arguments

    collapse all

    Distinct values from the specified field in the MongoDB collection, returned as a cell array. Values are returned from all documents in the collection, or from documents that match the specified query, if provided.

    Version History

    Introduced in R2026b