%% Update Documents in Collection
% Connect to MongoDB® using the MongoDB C++ interface and update documents in a collection. Find documents to update by using a MongoDB query. Specify the criteria for the update by using a MongoDB query. 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.
%
% Specify the employees collection. Create a MongoDB query to find employees in the department where the department identifier is set to 90. Then, create a MongoDB query to increase the value in the salary field by 5000.

collection = "employees";
findquery = "{""department_id"":90}";
updatequery = "{""$inc"":{""salary"":5000}}";

%
% Increase the salaries for all employees in the department using the MongoDB connection. The update function updates four documents in the collection.

n = update(conn,collection,findquery,updatequery)

%
% Close the MongoDB connection.

close(conn)

% Copyright 2020 The MathWorks, Inc.