%% Retrieve All Documents in Collection
% 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 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)

% 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}

% 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)

% Copyright 2020 The MathWorks, Inc.