%% Insert One Document into Collection as Structure
% Connect to MongoDB® using the MongoDB C++ interface and export one document from MATLAB® and insert it into a collection. Specify the document to insert as a structure. 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 one document as the document structure with three fields. Set the employee_id field to 28, department_id field to 80, and salary field to 200,000.

document.employee_id = 28;
document.department_id = 80;
document.salary = 200000;

%
% Specify the employees collection. Insert the document into the collection by using the MongoDB C++ interface connection. The insert function inserts one document into the collection.

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

%
% Close the MongoDB connection.

close(conn)

% Copyright 2020 The MathWorks, Inc.