Video Transcript (English)
Hello! In this video, you’ll learn how to replace a document in MongoDB.
Sometimes documents are incorrectly inserted into a collection. Luckily, it’s a simple process to replace those inserted documents within your collection. Let’s get started!
To replace a single document, we use the `replaceOne()` method.
This method accepts three arguments: a filter, a replacement document, and an options object. The options object is not required and won’t be taught in this video.
To understand how the `replaceOne` method works, let’s use an example.
Imagine you work for a publisher that has a database for released and unreleased computer science books. Unreleased books still get an entry in the database, but because they are incomplete, some of them have temporary titles or missing data.
We can replace any entry, or document, that has missing or incorrect data with an updated document that contains the accurate information. But we can keep the same _id, which is pretty convenient.
Let’s look at an example document.
Here we see the ISBN, publication date, and thumbnail URL all have placeholder information. This is because the document was created before the book was ready to publish.
To replace this placeholder document with an updated one, we’ll start by using the `replaceOne` method on the `books` collection.
Notice that we're using the document structure here, which is indicated by curly brackets.
We’ll want to identify which document we want to replace. To do this, we’ll specify the `_id` in our filter. You’ll recall from previous lessons that `_id`s are a good filter criteria because they are guaranteed to be unique.
Now, imagine that the publisher has up-to-date information about the placeholder document that they want to add to the database. In a scenario like this, it makes sense to replace the entire document because the updated information applies to most fields in the document.
We’ll replace the document with a different one by passing the replacement document as the second parameter to the `replaceOne()` method. The replacement document contains all the data (excluding the _id field) that will take the place of the current document.
When we run the command, the output shows us a matched count and modified count, both of which are 1. The matched count is the number of documents that matched our filter. The modified count refers to how many of those documents were modified.
Given this output, we can feel confident that our replacement was a success. But if we want to be sure, we can run `findOne()` on the document. This command locates a single document based on certain criteria. Again, the criteria is the `_id`.
Running this command allows us to confirm that the document has been updated, and that it has the same document `_id` as before.
Excellent work! Now let’s recap what you learned in this video.
You replaced a document in MongoDB by using `replaceOne()`. This method accepts a filter and a replacement document. In return, you’ll get output that contains the number of matched and modified documents.
