Update Data in MongoDB¶
Author: MongoDB Documentation Team
In this guide, you will update documents in a MongoDB database.
Time required: 10 minutes
What You’ll Need¶
- If you have not already installed a client (e.g. a driver, MongoDB Compass, or the
mongo
shell), complete the Connect to MongoDB guide before attempting this guide.
- If you have not already installed a client (e.g. a driver, MongoDB Compass, or the
mongo
shell), complete the Connect to MongoDB guide before attempting this guide. - Enable Auth on your local instance of MongoDB.
Warning
If you are running MongoDB locally and have not enabled authentication, your MongoDB instance is not secure.
- If you have not already installed a client (e.g. a driver, MongoDB Compass, or the
Complete the Import Data into MongoDB guide so that there is data in your database to update.
Procedure¶
Connect to your MongoDB instance.¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Select the operating system platform on which you are running the MongoDB client you have selected.
- Windows
- macOS
- Linux
Pass the URI to the mongo shell followed by the --password
option. You will then be prompted for your password.
Pass the URI to the mongo shell followed by the --password
option. You will then be prompted for your password.
Pass the URI to the mongo shell followed by the --password
option. You will then be prompted for your password.
If you wish to manually configure your Compass connection, load
Compass and select the New Connection
link. You will see a
form where you can enter connection information for MongoDB.
Atlas users can copy a URI string from the Atlas console into Compass. MongoDB Compass can detect whether you have a MongoDB URI connection string in your system clipboard and auto- populate the connection dialog from the URI.
See Set Up Atlas Connectivity for information on how to get the Atlas connection string URI into your copy buffer.
If Compass was already running when you copied the URI string, click the NEW CONNECTION button.

You will be prompted to populate the connection dialog. Click Yes.
You should then populate the password field with the proper password for your MongoDB user in the connection form.
Note
Errors related to connecting through Compass will appear in red at the top of the Connect screen.
It’s a good idea to put your connection code in a class so that it can be reused.
If your connection_string
starts with mongodb+srv, you need to install the dnspython module with
Now add code to call the class you just created.
For the MongoDB java driver 3.7 and beyond, use the MongoClients.create()
method.
For legacy drivers (prior to 3.7), use:
The MongoDB.Bson
package is used in CRUD operations, so
you’ll import it here.
Replace your password and any parameters surrounded by $[]
in the connection string in the code below.
For now, you will use the context.TODO().
Later you’ll configure the context specific to your requirements.
You won’t know if the connection has been successful until you
use the connection. A ping is one way you can test the
connection. This is a full example of a Go connection to
mongoDB, including a test ping
.
In your Go workspace and project folder, run build.
Now run the binary. For binaries that are not installed, you’ll have to specify the path.
If you’d like to run the resulting binary without specifying a path, install the binary you just built into your Go workspace.
Now run the code. “yourprojectname” is the name of the project
directory that contains the file with your main()
function.
For installed binaries use:
For binaries that are not installed, you’ll have to specify the path.
The default timeout for the Go driver to connect to the database is 30 seconds. In the event that you are unable to connect, you will see an error that resembles this:
Switch to the test
database.¶
In this guide, you will update documents in a collection in the
test
database.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
To switch to the test
database in the mongo
shell, type
If the database has not been created already, click the Create Database button.

Select the test
database on the left side of the Compass
interface. Compass will list all of the collections in the
database below the database name.

To access the test
database:
Switch to the test
database and
access the inventory
collection.
Within the connect block, set db
to the test
database.
To access the test
database:
Switch to the test
database and
access the inventory
collection.
To access the test
database:
Update a single document in the inventory
collection.¶
To change a field value, MongoDB provides update operators to modify values. Some update operators, including will create the specified field if the field does not exist in the document.
The following operation updates the first document with item
equal
to paper
. The operation uses:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
$set
to update thestatus
field and theuom
field embedded in thesize
document. To access the fields within embedded documents, the operation uses dot notation.$currentDate
to set thelastModified
field to the current date.
Copy the following filter into the Compass query bar and click Find:
Click the edit icon on the first document returned:
Expand the
size
field and update theuom
value tocm
. Click inside thestatus
field and click the plus button, then click Add field after status. Add alastModified
field, selectDate
as its type using the drop-down menu on the right, and input today’s date:Click Update.
$set
to update thestatus
field and theuom
field embedded in thesize
document. To access the fields within embedded documents, the operation uses dot notation.$currentDate
to set thelastModified
field to the current date.
Run the loop:
- set
to update the
status
field and theuom
field embedded in thesize
document. To access the fields within embedded documents, the operation uses dot notation. - currentDate
to set the
lastModified
field to the current date.
$set
to update thestatus
field and theuom
field embedded in thesize
document. To access the fields within embedded documents, the operation uses dot notation.$currentDate
to set thelastModified
field to the current date.
$set
to update thestatus
field and theuom
field embedded in thesize
document. To access the fields within embedded documents, the operation uses dot notation.$currentDate
to set thelastModified
field to the current date.
For completeness, the following example shows how you might wrap the update one operation with the asyncio event loop:
$set
to update thestatus
field and theuom
field embedded in thesize
document. To access the fields within embedded documents, the operation uses dot notation.$currentDate
to set thelastModified
field to the current date.
Before updating the data, you’ll need to assign the inventory
collection in the test
database to a variable:
Followed by the call to update:
Update multiple documents.¶
The following operation updates all of the documents with
quantity
value less than 50.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following filter into the Compass query bar and click Find:
For each document, click the edit icon:
Make the following changes:
- Expand the
size
field and update theuom
value tocm
. - Change the value of the
status
field toP
. - Click inside the
status
field and click the plus button, then click Add field after status. Add alastModified
field, selectDate
as its type using the drop-down menu on the right, and input today’s date:
- Expand the
Click Update.
For completeness, the following example shows how you might wrap the update many operation with the asyncio event loop:
Run the asyncio loop to execute both the update_one
and update_many
operations:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- Motor
- Other
The operation returns a WriteResult
document that contains the status
of the operation:
The operation returns an instance of
pymongo.results.UpdateResult
that contains the
status of the operation.
The operation returns an instance of com.mongodb.client.result.UpdateResult with the status of the operation.
The operation returns a promise that provides a result
containing the status of the operation.
The operations asynchronously return instances of
pymongo.results.UpdateResult
that contains the
status of the operation.
Upon successful execution, the operation returns an instance of UpdateResult that contains the status of the operation.
- Python
- Java (Sync)
- Motor
When you are done working with your MongoDB data, close your connection to MongoDB:
Summary¶
If you have successfully completed this guide, you have updated documents in MongoDB. In the next guide, you’ll see how to delete a document from a MongoDB collection.
What’s Next¶
In this guide, you will delete documents from a MongoDB database.
See Also¶
For other CRUD guides:
- Insert Data into MongoDB
- Read Data using Operators and Compound Queries
- Read Data from MongoDB With Queries
- Delete Data from MongoDB
For method reference: