10.4. Updating documents in MongoDB: Working with update operators in MongoDB

Página 48

Updating documents is a common task when working with databases. In MongoDB, this task is accomplished using update operators. In this chapter, we'll explore how to work with update operators in MongoDB.

Before diving into the details, it's important to understand what update operators are. In simple terms, update operators are instructions that MongoDB uses to modify the values ​​of documents in a collection. They are used in conjunction with the update() or findAndModify() method to change data in a document.

Update Operators in MongoDB

In MongoDB, we have several update operators available. Let's start with the most common ones:

  • $set: This operator replaces the value of a field with a new value. If the field does not exist in the document, $set will add a new field with the specified value.
  • $unset: This operator removes a field from a document.
  • $inc: This operator increments the value of a field by a specified amount. If the field does not exist, $inc will create a new field with the specified value.
  • $mul: This operator multiplies the value of a field by a specified value.
  • $rename: This operator renames a field.

Updating Documents in MongoDB

To update a document in MongoDB, we use the update() method. The update() method accepts three arguments: the selection criteria, the update operator, and an options object. The selection criteria is used to select the document to be updated. The update operator is used to specify the modification to be made. The options object is used to specify additional options, such as whether the operation should be a multi-document update or an upsert operation.

For example, to update the "name" field of a document in the "users" collection, you can use the following code:

db.users.update(
  { _id: 1 },
  { $set: { name: "New Name" } }
)

This code will look for the document in the "users" collection where the "_id" field is equal to 1 and update the "name" field to "New Name".

Working with Update Operators

Let's see how we can use some of the update operators mentioned above.

To use the $set operator, you can do the following:

db.users.update(
  { _id: 1 },
  { $set: { age: 30 } }
)

This code will update the "age" field to 30 in the document where the "_id" field equals 1.

To use the $unset operator, you can do the following:

db.users.update(
  { _id: 1 },
  { $unset: { age: "" } }
)

This code will remove the "age" field in the document where the "_id" field is equal to 1.

To use the $inc operator, you can do the following:

db.users.update(
  { _id: 1 },
  { $inc: { age: 1 } }
)

This code will increment the value of the "age" field by 1 in the document where the "_id" field is equal to 1.

To use the $mul operator, you can do the following:

db.users.update(
  { _id: 1 },
  { $mul: { age: 2 } }
)

This code will multiply the value of the "age" field by 2 in the document where the "_id" field is equal to 1.

To use the $rename operator, you can do the following:

db.users.update(
  { _id: 1 },
  { $rename: { "age": "newAge" } }
)

This code will rename the "age" field to "newIdity" in the document where the "_id" field is equal to 1.

In summary, update operators in MongoDB are powerful tools that allow us to modify the data in our documents efficiently and effectively. By mastering these operators, you will be able to manipulate your data in MongoDB with ease and precision.

Now answer the exercise about the content:

What is the role of update operators in MongoDB and how are they used?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

4910.5. Updating documents in MongoDB: Updating embedded documents in MongoDB

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text