Main Content

find

Retrieve documents in MongoDB collection

Since R2021b

Description

example

documents = find(conn,collection) returns all documents in a collection by using the MongoDB® C++ interface connection.

example

documents = find(conn,collection,Name=Value) specifies additional options using one or more name-value arguments. For example, Limit=10 limits the number of documents returned to 10.

Examples

collapse all

Connect to MongoDB® using the MongoDB C++ interface, retrieve all documents in a collection, and import them into MATLAB®. In this example, each document in the collection represents an employee.

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

server = "dbtb01";
port = 27017;
dbname = "mongotest";
conn = mongoc(server,port,dbname)
conn = connection with properties:
           Database: "mongotest"
           UserName: ""
             Server: "dbtb01"
               Port: 27017
    CollectionNames: [13×1 string]

conn is the connection 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 13 document collections.

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.

Specify the employees collection for document retrieval. Retrieve all documents in the collection by using the MongoDB connection. documents is a cell array of structures.

collection = "employees";
documents = find(conn,collection);

Display the first document in the collection. Each document is a structure.

documents{1}
ans = struct with fields:
              _id: '5d8ccb9c961c96252819ea63'
      employee_id: 100
       first_name: 'Steven'
        last_name: 'King'
            email: 'SKING'
     phone_number: '515.123.4567'
        hire_date: '2003-06-17 00:00:00.0'
           job_id: 'AD_PRES'
           salary: 24000
    department_id: 90
        temporary: 0

The structure fields are:

  • Unique identifier

  • Employee identifier

  • First name

  • Last name

  • Email

  • Phone number

  • Hire date

  • Job name

  • Employee salary

  • Department identifier

  • Temporary flag

Close the MongoDB connection.

close(conn)

Connect to MongoDB® using the MongoDB C++ interface, retrieve all documents in a MongoDB query on a collection in the database, and import them into MATLAB®. In this example, each document in the collection represents an employee.

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

server = "dbtb01";
port = 27017;
dbname = "mongotest";
conn = mongoc(server,port,dbname)
conn = connection with properties:
           Database: "mongotest"
           UserName: ""
             Server: "dbtb01"
               Port: 27017
    CollectionNames: [13×1 string]

conn is the connection 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 13 document collections.

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.

Specify the employees collection for document retrieval. Create the MongoDB query as a string scalar that contains a JSON-style string. This query retrieves all employees in the department that has the department identifier 80.

collection = "employees";
mongoquery = "{""department_id"":80}";

Retrieve all documents in the MongoDB query on the employees collection by using the MongoDB connection. documents is a cell array that contains a structure for each document returned by the query.

documents = find(conn,collection,Query=mongoquery);

Close the MongoDB connection.

close(conn)

Connect to MongoDB® using the MongoDB C++ interface and retrieve documents in a MongoDB query on a collection in the database. Then, sort the results by a field in the documents. In this example, each document in the collection represents an employee.

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

server = "dbtb01";
port = 27017;
dbname = "mongotest";
conn = mongoc(server,port,dbname)
conn = connection with properties:
           Database: "mongotest"
           UserName: ""
             Server: "dbtb01"
               Port: 27017
    CollectionNames: [13×1 string]

conn is the connection 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 13 document collections.

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.

Specify the employees collection for document retrieval. Create the MongoDB query as a string scalar that contains a JSON-style string. This query retrieves all employees in the department that has the department identifier 80.

collection = "employees";
mongoquery = "{""department_id"":80}";

Create the sort query as a string scalar that contains a JSON-style string. Sort the documents by the salary field.

sortquery = "{""salary"":1.0}";

Retrieve all documents in the MongoDB query on the employees collection by using the MongoDB connection, and sort the documents. documents is a cell array that contains a structure for each document returned by the query. The documents are sorted by salary in increasing order.

documents = find(conn,collection,Query=mongoquery,Sort=sortquery);

Display the data for the first two employees sorted by salary. The salary for the second employee is higher than the first employee.

documents{1:2}
ans = struct with fields:
               _id: '5d8ccc16961c96252819ea68'
       employee_id: 148
        first_name: 'Gerald'
         last_name: 'Cambrault'
             email: 'GCAMBRAU'
      phone_number: '011.44.1344.619268'
         hire_date: '2007-10-15 00:00:00.0'
            job_id: 'SA_MAN'
            salary: 11000
    commission_pct: 0.3000
        manager_id: 100
     department_id: 80
         temporary: 0

ans = struct with fields:
               _id: '5d8ccc12961c96252819ea67'
       employee_id: 147
        first_name: 'Alberto'
         last_name: 'Errazuriz'
             email: 'AERRAZUR'
      phone_number: '011.44.1344.429278'
         hire_date: '2005-03-10 00:00:00.0'
            job_id: 'SA_MAN'
            salary: 12000
    commission_pct: 0.3000
        manager_id: 100
     department_id: 80
         temporary: 0

Close the MongoDB connection.

close(conn)

Connect to MongoDB® using the MongoDB C++ interface and retrieve all documents in a collection. Specify the fields to retrieve for each document. Import the documents into MATLAB®. In this example, each document in the collection represents an employee.

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

server = "dbtb01";
port = 27017;
dbname = "mongotest";
conn = mongoc(server,port,dbname)
conn = connection with properties:
           Database: "mongotest"
           UserName: ""
             Server: "dbtb01"
               Port: 27017
    CollectionNames: [13×1 string]

conn is the connection 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 13 document collections.

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.

Specify the employees collection for document retrieval. Specify the fields to retrieve for each document by using a string scalar that contains a JSON-style string. For this example, return the department_id and salary fields.

collection = "employees";
fields = "{""department_id"":1.0,""salary"":1.0}";

Retrieve all documents in the collection. Use the name-value argument Projection to retrieve the specified fields for each document. documents is a cell array.

documents = find(conn,collection,Projection=fields);

Display the first document in the results. In addition to the unique identifier for the document, the document contains only the specified fields.

documents{1}
ans = struct with fields:
              _id: '5d8ccb9c961c96252819ea63'
           salary: 24000
    department_id: 90

Close the MongoDB connection.

close(conn)

Connect to MongoDB® using the MongoDB C++ interface and retrieve a specific number of documents in a collection in the database. Return documents from a specific position in the results using an offset value. Import the documents into MATLAB®. In this example, each document in the collection represents an employee.

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

server = "dbtb01";
port = 27017;
dbname = "mongotest";
conn = mongoc(server,port,dbname)
conn = connection with properties:
           Database: "mongotest"
           UserName: ""
             Server: "dbtb01"
               Port: 27017
    CollectionNames: [13×1 string]

conn is the connection 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 13 document collections.

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.

Specify the employees collection for document retrieval.

collection = "employees";

Use the name-value argument Skip to skip the first five documents in the collection. Then, use the name-value argument Limit to return the next 10 documents in the collection. documents is a cell array that contains 10 documents.

documents = find(conn,collection,Skip=5,Limit=10);

Close the MongoDB connection.

close(conn)

Input Arguments

collapse all

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

Collection name, specified as a string scalar.

Example: "taxidata"

Data Types: string

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: Skip=5,Limit=10 skips the first five documents in a collection and returns the next 10 documents.

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

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

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

Example: Query="{""_id"":{""$oid"":""593fec95b78dc311e01e9204""}}" queries the database for the document that has the identifier 593fec95b78dc311e01e9204.

Data Types: char | string

Fields to retrieve in each document, specified as a string scalar or character vector. Specify a JSON-style string to describe the fields.

Example: Projection="{""department"":1.0,""salary"":1.0}" returns the department and salary fields.

Data Types: char | string

Sort field for documents, specified as a string scalar or character vector. Specify a JSON-style string to describe the sort field.

Example: Sort="{""department"":1.0}" sorts the returned documents by the department field.

Data Types: char | string

Offset from the beginning of the returned documents, specified as a numeric scalar.

Example: Skip=5 skips the first five returned documents.

Data Types: double

Number of documents to return, specified as a numeric scalar.

Example: Limit=10 returns 10 documents.

Data Types: double

Output Arguments

collapse all

Documents in a MongoDB collection or query on a collection, returned as a structure, structure array, or cell array of structures.

Each JSON-style document is represented as a structure. The find function returns a:

  • Structure for one document

  • Structure array for multiple documents containing the same fields

  • Cell array of structures for multiple documents containing different fields

Version History

Introduced in R2021b