update
Description
returns the number of documents updated in a collection using the MongoDB® C++ interface connection. Use MongoDB queries to find and update documents.n
= update(conn
,collection
,findquery
,updatequery
)
Examples
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 = connection with properties:
Database: "mongotest"
UserName: ""
Server: "dbtb01"
Port: 27017
CollectionNames: [13×1 string]
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)
ans = logical
1
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)
n = int64
4
Close the MongoDB connection.
close(conn)
Input Arguments
conn
— MongoDB C++ interface connection
connection
object
MongoDB C++ interface connection, specified as a connection
object.
collection
— Collection name
string scalar
Collection name, specified as a string scalar.
Example: "taxidata"
Data Types: string
findquery
— MongoDB find query
string scalar | character vector
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
updatequery
— MongoDB update query
string scalar | character vector
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
n
— Number of documents updated
numeric scalar
Number of documents updated in a collection in the database, returned as a numeric scalar.
Version History
Introduced in R2021b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)