Welcome to the first lesson of "A Closer Look at Update Operations in MongoDB". This course builds upon your understanding of basic CRUD operations and data modeling. You've already covered read operations in depth. In this lesson, we will refresh your knowledge of updateOne
, updateMany
, and the $set
operator. Additionally, we'll introduce you to other update operators, which will be discussed in detail in upcoming lessons.
As you may recall from earlier in this course path, there are two main methods for updating documents in a MongoDB database: updateOne
for updating a single document and updateMany
for updating multiple documents at once. Here's the syntax for both methods:
1db.collection.updateOne(filter, update, options)
2db.collection.updateMany(filter, update, options)
The filter
parameter specifies which documents will be updated, while the update
parameter defines how the documents will be updated. The options
parameter is optional and can be used to specify additional settings for the update operation.
Below is a single code snippet demonstrating how to use updateOne
and updateMany
:
mongodb1use comic_book_store_db 2 3// updateOne example 4db.comic_books.updateOne( 5 { title: "Iron Man" }, 6 { $set: { stock_quantity: 20 } } 7); 8 9// updateMany example 10db.comic_books.updateMany( 11 { published_year: { $lt: 1980 } }, 12 { $set: { is_featured: true, stock_quantity: 50 } } 13);
In the updateOne
example, we update the stock_quantity
for the comic book titled "Iron Man". In the updateMany
example, we update all comic books published before 1980, setting their is_featured
status to true and their stock_quantity
to 50.
To update a document, you can use various operators, not just $set
. Here is a list of update operators in MongoDB with a brief explanation of each:
- $set: Sets the value of a field in a document.
- $unset: Removes a field from a document.
- $inc: Increments the value of a field by a specified amount.
- $mul: Multiplies the value of a field by a specified amount.
- $rename: Renames a field.
- $min: Updates the field only if the specified value is less than the existing field value.
- $max: Updates the field only if the specified value is greater than the existing field value.
- $currentDate: Sets the value of a field to the current date.
In this lesson, we will focus on refreshing our knowledge of the $set
operator. Other operators will be covered in subsequent lessons.
For more detailed information about each operator, refer to the official documentation: MongoDB Update Operators.
To update nested fields within embedded documents, you can use dot notation, similar to how it’s used in read queries. Remember to enclose the field names in quotes when using dot notation to avoid errors.
mongodb1use comic_book_store_db 2 3db.comic_books.updateOne( 4 { title: "Iron Man" }, 5 { $set: { "writer.name": "Stan Lee" } } 6);
In this example, we update the name
field within the writer
embedded document for the comic book titled "Iron Man".
In this lesson, we revisited the updateOne
and updateMany
functions, along with the $set
operator. We also introduced other update operators such as $unset
, $inc
, $mul
, $rename
, $min
, $max
, and $currentDate
, which will be explored in more detail in future lessons. Make sure to review these concepts as they will form the foundation for the upcoming practices and lessons in this course. Happy coding!