update

Update one or multiple documents in MongoDB collection

Description

example

n = update(conn,collection,findquery,updatequery) returns the number of documents updated in a collection using the MongoDB® connection. Use MongoDB queries to find and update documents.

Examples

collapse all

Connect to MongoDB 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. Here, the collection represents employee data.

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

server = "dbtb01";
port = 27017;
dbname = "mongotest";
conn = mongo(server,port,dbname)
conn = 

  mongo with properties:

               Database: 'mongotest'
               UserName: ''
                 Server: {'dbtb01'}
                   Port: 27017
        CollectionNames: {'airlinesmall', 'employee', 'largedata' ... and 3 more}
         TotalDocuments: 23485919

conn is the mongo 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 six document collections. The first three collection names are airlinesmall, employee, and largedata.

  • This database contains 23,485,919 documents.

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 employee collection. Create a MongoDB query to find employees in the Sales department. Then, create a MongoDB query to increase the value in the salary field by 5000.

collection = "employee";
findquery = '{"department":"Sales"}';
updatequery = '{$inc:{"salary":5000}}';

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

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

     7

Close the MongoDB connection.

close(conn)

Input Arguments

collapse all

MongoDB connection, specified as a mongo object.

Collection name, specified as a string scalar.

Example: "taxidata"

Data Types: string

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

Example: '{"department":"Sales"}' finds all documents where the department field is equal to Sales.

Example: '{"_id":{$oid:"593fec95b78dc311e01e9204"}}' finds the document that has the identifier 593fec95b78dc311e01e9204.

Data Types: char | string

MongoDB update query, specified as a string scalar or character vector. Use a JSON-style string to specify the criteria for the update.

Example: '{$inc:{"salary":5000}}' increases the values in the salary field by 5000.

Data Types: char | string

Output Arguments

collapse all

Number of documents updated in a collection in the database, returned as a numeric scalar.

Introduced in R2017b