Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

distinct

Retrieve distinct values for field in MongoDB collection

Syntax

values = distinct(conn,collection,field)
values = distinct(conn,collection,field,'Query',mongoquery)

Description

example

values = distinct(conn,collection,field) returns distinct values for a field in a collection by using the MongoDB® connection.

example

values = distinct(conn,collection,field,'Query',mongoquery) returns distinct values for a field in an executed MongoDB query on a collection.

Examples

collapse all

Connect to MongoDB and retrieve a distinct set of values for a field in a collection of documents. Here, each document in the collection represents an employee.

Create a MongoDB connection to the database mongotest. Here, the database server dbtb01 hosts this database using port number 27017.

server = "dbtb01";
port = 27017;
dbname = "mongotest";
conn = mongo(server,port,dbname)
conn = 

  mongo with properties:

               Database: 'mongotest'
               UserName: ''
                 Server: {'dbtb01'}
                   Port: 27017
        CollectionNames: {'airlinesmall', 'employee', 'largedata' ... and 3 more}
         TotalDocuments: 23485919

conn is the mongo object that contains the MongoDB connection. The object properties contain information about the connection and the database.

  • The database name is mongotest.

  • The user name is blank.

  • The database server is dbtb01.

  • The port number is 27017.

  • This database contains six document collections. The first three collection names are airlinesmall, employee, and largedata.

  • This database contains 23,485,919 documents.

Verify the MongoDB connection.

isopen(conn)
ans =

  logical

   1

The database connection is successful because the isopen function returns 1. Otherwise, the database connection is closed.

Retrieve a distinct set of values for a field in a document collection. Here, retrieve distinct salaries for all employees. values is a cell array of doubles.

collection = "employee";
field = "salary";
values = distinct(conn,collection,field);

Display the first three salaries in the cell array.

values{1:3}
ans =

       60000


ans =

       50000


ans =

       55000

Close the MongoDB connection.

close(conn)

Connect to MongoDB and retrieve a distinct set of values for a field in a MongoDB query. Here, each document in the collection represents an employee.

Create a MongoDB connection to the database mongotest. Here, the database server dbtb01 hosts this database using port number 27017.

server = "dbtb01";
port = 27017;
dbname = "mongotest";
conn = mongo(server,port,dbname)
conn = 

  mongo with properties:

               Database: 'mongotest'
               UserName: ''
                 Server: {'dbtb01'}
                   Port: 27017
        CollectionNames: {'airlinesmall', 'employee', 'largedata' ... and 3 more}
         TotalDocuments: 23485919

conn is the mongo object that contains the MongoDB connection. The object properties contain information about the connection and the database.

  • The database name is mongotest.

  • The user name is blank.

  • The database server is dbtb01.

  • The port number is 27017.

  • This database contains six document collections. The first three collection names are airlinesmall, employee, and largedata.

  • This database contains 23,485,919 documents.

Verify the MongoDB connection.

isopen(conn)
ans =

  logical

   1

The database connection is successful because the isopen function returns 1. Otherwise, the database connection is closed.

Create a JSON-style query as a character vector that contains a JSON-style string. This query sets the department field equal to the value Sales.

mongoquery = '{"department":"Sales"}';

Use the MongoDB query on the employee collection to retrieve a distinct set of values for a field. Here, retrieve distinct salaries of all employees in the Sales department. values is a cell array of doubles.

collection = "employee";
field = "salary";
values = distinct(conn,collection,field,'Query',mongoquery)
values =

  1×3 cell array

    {[60000]}    {[64440]}    {[66000]}

Close the MongoDB connection.

close(conn)

Input Arguments

collapse all

MongoDB connection, specified as a mongo object.

Collection name, specified as a string scalar.

Example: "taxidata"

Data Types: string

Field in a collection, specified as a string scalar.

Example: "department"

Data Types: string

MongoDB query, specified as a string scalar or character vector. Specify a JSON-style string to query the database.

Example: '{"department":"Sales"}' queries the database for documents where the department field is equal to Sales.

Example: '{salary: {$gt: 90000}}' queries the database for documents where the value of the salary field is greater than 90000.

Data Types: char | string

Output Arguments

collapse all

Distinct values of a field in a MongoDB collection or query, specified as a cell array. The cell array can contain numeric scalars for numeric data, character vectors for text data, and structures for nested documents.

Introduced in R2017b

Was this topic helpful?