Lesson 5
Replacing and Upserting Documents in MongoDB
Introduction

Welcome to the lesson! You've already mastered methods updateOne and updateMany, as well as basic update operators in MongoDB, such as $set, $unset, $rename, and more. This lesson will delve into the replaceOne method and the upsert option, highlighting how these features can simplify data management in MongoDB.

Replacing Documents

You already know how to update documents by adding, removing, renaming fields, or changing field values. But what if you simply want to replace the entire document? This is where the replaceOne method comes in handy. It replaces the entire document without requiring any update operators.

Here’s the syntax for replaceOne:

1db.collection.replaceOne(<filter>, <replacement>, <options>)

The filter parameter specifies the criteria for selecting the document to be replaced. The replacement parameter is the new document that will replace the existing one. The optional options parameter allows you to pass additional options, including the upsert flag, which we'll cover shortly.

"replaceOne" in Action

Let’s replace an old document with a new one in the comic_book_store_db dataset. Suppose we have new comprehensive data about the first issue of "Deadpool" and want to replace the old document with this new information.

JavaScript
1use comic_book_store_db 2 3db.comic_books.replaceOne( 4 { title: "Deadpool", issue_number: 1 }, 5 { 6 title: "Deadpool", 7 rating: "Mature", 8 issue_number: 1, 9 published_year: 1991, 10 genres: ["Action", "Comedy", "Superhero"], 11 writer: { name: "Fabian Nicieza", nationality: "American" }, 12 artist: { name: "Rob Liefeld", nationality: "American" }, 13 // ... 14 } 15)

In this code, the replaceOne method searches for the document with title: "Deadpool" and issue_number: 1. If it finds the document, it replaces it with the new document provided. The old document is entirely replaced by the new one, with all the specified fields. However, the _id field remains unchanged as it uniquely identifies the document in the collection.

Understanding Upsert

The upsert option combines the actions of updating and inserting. It works with updateOne, updateMany, and replaceOne. If a document matching the filter criteria does not exist, the upsert option will insert a new document based on the criteria and update information provided.

The syntax looks like this:

1db.collection.updateOne(<filter>, <update>, { upsert: true } ) 2db.collection.updateMany(<filter>, <update>, { upsert: true } ) 3db.collection.replaceOne(<filter>, <replacement>, { upsert: true } )
`upsert` in Action

Let's see the upsert option in action. Suppose we want to add or update the first issue of "Deadpool" in our comic book collection. If the document doesn't exist, we want MongoDB to insert it automatically.

JavaScript
1use comic_book_store_db 2 3db.comic_books.updateOne( 4 { title: "Deadpool", issue_number: 1 }, 5 { 6 $set: { 7 characters: [ 8 { name: "Deadpool", alter_ego: "Wade Wilson", abilities: ["Regeneration", "Expert marksman", "Martial artist"] }, 9 { name: "Weasel", role: "Deadpool's friend and weapon supplier" }, 10 { name: "Vanessa Carlisle", role: "Deadpool's love interest" } 11 ] 12 } 13 }, 14 { upsert: true } 15)

In this example, updateOne searches for a document with title: "Deadpool" and issue_number: 1. The $set operator specifies the fields to update if the document exists. If the document doesn't exist, MongoDB will create it using the specified fields. In this case, it will create a document with title, issue_number, and characters. The key here is the upsert: true option, which ensures the document is inserted if it isn’t found.

Summary

In this lesson, you learned about the replaceOne method which allows you to replace entire documents seamlessly, and the upsert option which combines update and insert operations to prevent data duplication and simplify updates. Understanding these advanced features will make your MongoDB data management more efficient. Keep practicing these commands to reinforce your knowledge. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.