10.6. Updating documents in MongoDB: Conditional updating documents in MongoDB

Página 50

In section 10.6 of our e-book course on creating and maintaining a MongoDB database from basics to advanced, we will cover a very important topic: updating documents in MongoDB. More specifically, we will focus on conditional document updating.

To begin with, it is important to understand what it means to update a document in MongoDB. In simple terms, updating a document involves changing one or more fields in the document. This can be done using several methods, but the most common is the update() method.

The update() method requires two arguments: the selection criteria (which identifies which documents to update) and the updates to be applied. For example, if we want to update the "name" field of a document in a collection called "users", the code would be something like this:

db.users.update(
   { name: "John" },
   { $set: { name: "João Silva" } }
)

This code searches the "users" collection for a document whose "name" field is "João" and changes the value of that field to "João Silva".

However, what if we want to do a conditional update? For example, if we want to update the "name" field only if the "age" field is greater than 18? This is what conditional document updating in MongoDB allows you to do.

To perform a conditional update, we use the same basic structure as the update() method, but we add a condition to the selection criteria. For example:

db.users.update(
   { name: "João", age: { $gt: 18 } },
   { $set: { name: "João Silva" } }
)

In this example, the update will only be applied if the document has a "name" field equal to "John" and an "age" field greater than 18. If these conditions are not met, the document will not be updated.

p>

There are many operators that can be used to create conditions in MongoDB. Some of the most common include $gt (greater than), $lt (less than), $gte (greater than or equal to), $lte (less than or equal to), $eq (equal to), $ne (not equal to) , $in (within an array of values), and $nin (not within an array of values).

In addition, it is possible to combine multiple conditions using logical operators such as $and (and), $or (or), $not (not) and $nor (neither). For example, the following code updates the "name" field only if the "age" field is greater than 18 and the "city" field is equal to "São Paulo":

db.users.update(
   { $and: [ { age: { $gt: 18 } }, { city: "São Paulo" } ] },
   { $set: { name: "João Silva" } }
)

In summary, conditional document update in MongoDB is a powerful tool that allows you to make changes to documents based on specific criteria. This makes MongoDB a flexible and efficient option for managing databases.

In the next section of our eBook course, we'll explore other aspects of updating documents in MongoDB, including updating multiple documents at once and creating new fields during updating. Stay tuned!

Now answer the exercise about the content:

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

You missed! Try again.

Next page of the Free Ebook:

5110.7. Updating documents in MongoDB: Using the findAndModify command 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