mongo-db
  1. mongo-db-update-documents

Update Documents - (CRUD Operations)

MongoDB provides powerful ways to update documents using various methods. In this tutorial, we will cover updating MongoDB documents using update and replace methods.

Syntax

Update Method Syntax

The update() method is used to perform update operations in MongoDB. Here is the basic syntax:

db.collection.update(
   <query>,
   <update>,
   {
     <optional parameters>
   }
)
  • query: This parameter specifies the criteria for selecting the document(s) to update.
  • update: This parameter specifies the modification to apply to the selected document(s) or replacement document.
  • optional parameters: This parameter specifies options, such as which documents to update, sorting, limiting, and more.

Replace Method Syntax

The replaceOne() method is used to replace a single document that matches a specified filter in MongoDB. Here is the basic syntax:

db.collection.replaceOne(
   <filter>,
   <replacement>,
   {
     <optional parameters>
   }
)
  • filter: This parameter specifies the selection criteria for the document to replace.
  • replacement: This parameter specifies the new document that will replace the selected document.
  • optional parameters: This parameter specifies options, such as which documents to replace, sorting, limiting, and more.

Example

Update Method Example

Let's say we have a collection named students with documents that have the following structure:

{
   "_id": ObjectId("60ad774a1a6bdfc2d41ab2e5"),
   "name": "John",
   "age": 25,
   "status": "active",
   "courses": ["math", "english", "history"]
}

Now, let's update the document with the name "John" and add a new course to the courses field. Here's how to do it:

db.students.update(
   { name: "John" },
   { $push: { courses: "biology" } }
)

This query will select the document with the name "John" and add "biology" to the courses array. So, the updated document will look like this:

{
   "_id": ObjectId("60ad774a1a6bdfc2d41ab2e5"),
   "name": "John",
   "age": 25,
   "status": "active",
   "courses": ["math", "english", "history", "biology"]
}

Replace Method Example

Let's say we have the same students collection, and we want to replace the entire document with a new one. Here's how to do it using the replaceOne() method:

db.students.replaceOne(
   { name: "John" },
   {
      "name": "Jane",
      "age": 23,
      "status": "inactive",
      "courses": ["biology"]
   }
)

This query will select the document with the name "John" and replace it with a new document containing the fields "name": "Jane", "age": 23, "status": "inactive", and "courses": ["biology"].

Output

The output of the queries will be a confirmation of a successful update or replacement of the document. The new or updated document can be retrieved by querying the collection after the update or replace operation.

Explanation

The update and replace methods in MongoDB are powerful tools for modifying documents in a collection. They can be used to add new fields, modify existing fields, or replace entire documents. The update method uses $set, $push, and other operators to update specific fields or add elements to arrays. The replace method replaces the entire document with a new one.

Use

Updating and replacing documents in mongodb are essential skills when working with databases. These methods allow you to make changes to documents to keep them up-to-date and accurate. Typical use cases of these methods include updating user profiles, modifying product information, or adding new content to a blog.

Important Points

  • MongoDB provides two main methods for updating and replacing documents: update() and replaceOne().
  • The update() method allows you to update specific fields in a document or add elements to arrays.
  • The replaceOne() method replaces a single document with a new one.
  • These methods are useful for keeping documents accurate and up-to-date in a MongoDB collection.

Summary

In this tutorial, we covered how to update and replace MongoDB documents using the update() and replaceOne() methods, respectively. We explained their syntax, provided examples, highlighted their respective outputs and functionalities, and their significance in working with databases in MongoDB.

Published on: