%% Insert Multiple Documents into Collection as Structure Array
% Connect to MongoDB® using the MongoDB C++ interface and export multiple documents from MATLAB® and insert them into a collection. Specify documents to insert as a structure array. In this example, the collection represents employee data.
%
% 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.
%
% Create two documents as structures with these fields: employee_id, department_id, and salary. For the employee1 structure, set the employee_id field to 26, department_id field to 80, and salary field to 100,000. For the employee2 structure, set the same fields to the values 27, 90, and 150,000 respectively. Create the documents structure array from these documents.

employee1.employee_id = 26;
employee1.department_id = 80;
employee1.salary = 100000;

employee2.employee_id = 27;
employee2.department_id = 90;
employee2.salary = 150000;

documents = [employee1 employee2];

%
% Specify the employees collection. Insert documents into the collection using the MongoDB connection. The insert function inserts two documents into the collection.

collection = "employees";
n = insert(conn,collection,documents)

%
% Close the MongoDB connection.

close(conn)

% Copyright 2020 The MathWorks, Inc.