Updating and replacing documents
1. Updating a single document
In this lesson, you'll discover how to update documents in a MongoDB collection.2. Why update or replace?
In any real-world application, data doesn’t just get added—it evolves. We correct mistakes, add missing info, or even restructure entire documents. That’s why MongoDB gives us tools to update existing documents or replace them entirely.3. Updating a single document
To update a single document, you first define a filter to find the document you want to change. This works just like with .find(). Here, we’re looking for the movie titled la la land. Next, we create an update object. This tells MongoDB what change we want to make. Here, we use the $set operator to set the release_year field to the value 2016. Other operators exist, such as `$unset` to remove a field, but we won't touch on those in this course. Finally, we call .update_one(), passing in both the query filter and the update. MongoDB looks for the document, applies the change and returns a result object. We can check the modified_count to see how many documents were actually updated. If the movie was found and updated, it should return 1. Even if your query filter matches more than one document, update_one() will still only update one document, namely, the first match it finds.4. Updating multiple documents
If you intend on updating several matching documents, you should use .update_many() instead. Just like with .update_one(), we start by defining a filter. Here, we’re looking for all movies where the genre is "comedy". This filter will match multiple documents, but that's the point! Next, we define the update operation using $set. We want to add a new field, is_funny, and set it to True. Then we call .update_many() with the filter and the update. MongoDB applies the change to every matching document. Finally, we print out modified_count and see that in total, 9 documents got updated.5. Replacing a full document
Sometimes, you don’t just want to update a few fields; you want to replace the entire document. That’s when you use .replace_one(). You start with a filter, just like with .update_one(), to find the document you want to replace. Then, you pass in the full replacement document. MongoDB will remove the original and store the new one in its place. Watch out: anything you leave out in the replacement will be permanently removed. So make sure to include all the fields you want to keep. Also note: the original document’s _id is preserved. You don't have to include it in the replacement explicitly, but if you do, it needs to match the existing document id. In this example, we replace the original lion king with the updated 2019 version. Except for the id, it's a full replacement.6. Let's practice!
Updates and replacements keep your data clean, current, and useful. Let’s put them into practice.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.