Video Transcript (English)
Hello!
In this video, you’ll learn how to perform single and multiple document updates in MongoDB with a Python application by using PyMongo’s update_one() and update_many() methods.
You’ll learn how to update the value of an existing field, and how to define a new field and value in an update.
First, let’s update a single document by using the update_one() method.
We use the update_one() method when we need to update a single document that matches some specified criteria.
The syntax for update_one() is db.collection.update_one().
update_one() has two required arguments: a filter document that specifies the selection criteria, and an update document that specifies the modifications.
To demonstrate update_one(), we’ll work with a database named `bank` that contains an `accounts` collection.
We want to increase the balance of a specific account by $100.
Here’s the document that contains the account that we need to update: Kate Stone’s checking account.
We’ve already started a Python file and connected to our Atlas cluster with MongoClient.
We have a reference to the `bank` database and a reference to the `accounts` collection.
First, we define the filter argument that we’ll pass to update_one().
We create a filter that matches the document to update by its _id value.
We’ve assigned the filter to a variable named document_to_update.
To work with ObjectIds in PyMongo, we need to import the ObjectId class from PyMongo’s bson package. We add this statement to our imports.
Next, we write the update modification, which increments the balance by $100.
Note that we’re using the $inc update operator to increment the `balance` field by the specified amount.
We assign the update to the `add_to_balance` variable.
Now we’re ready to write the expression that modifies the target document.
We call the update_one method on the `accounts_collection` object.
We pass in the two required arguments, first the filter, document_to_update, and then the update, add_to_balance.
The update_one() method finds the first document that matches the filter and applies the specified modifications.
It returns a result object that includes the number of documents that were modified, which will be either zero or one.
We assign the result of the update operation to a ‘result’ variable.
Then we write the logic to print a confirmation of the number of documents that were updated, and the updated data.
We’re also printing the document before the update, so we can see the change in the balance.
Let’s run the code. The output shows the original document, confirms that one document was updated, and prints the updated data. Great!
Now let’s go through an example of how to use update_many().
We use update_many() to update multiple documents in a single operation.
Just like the update_one() method, update_many() has two required arguments: a filter document and an update document that specifies the modifications to apply.
In this example, we want to add a `minimum_balance` field to every savings account, and set its value to $100.
To do this, we’ll use the update_many() method. And, this time,we’ll define a new field and value in an update.
We’ve started a new Python file, connected to our Atlas cluster with MongoClient, and created references to the database and collection.
First, we define our filter. We set the account_type to savings, and assign the filter to the select_accounts variable.
Then we write the update modification, which sets the value of the `minimum_balance` field to 100.
Note that we’re using the $set field update operator. If the specified field does not exist, $set will add a new field with the specified value. Because the `minimum_balance` field doesn’t currently exist in our documents, it will be added by this update.
We assign the update to the `set_field` variable.
Now we’re ready to write the expression that modifies the target documents.
We call the `update_many` method on the `accounts_collection` object.
We pass in the two required arguments, first the filter, select_accounts, and then the update, set_field.
The update_many() method returns a result, which gives us access to both the number of documents that matched the filter, as well as the number of documents that were modified.
We assign the result of the insert operation to a variable named `result.`
Then we use the result to print out a confirmation of the number of documents that matched and were modified by the operation.
We also print out one sample savings account document after the update.
Finally, we close the database connection.
Now let’s run the code. The output shows that 25 documents matched the filter and were updated.
It also shows that our sample savings account document contains the new `minimum_balance` field, and that its value is set to 100.
Well done!
In this video, you learned how to perform single and multiple document updates.
You used a query filter to match the documents to update, and then used update operators to update those documents.