Course

Updating Documents - Operators

In the previous lesson, we learned basic ways to update documents in Mongoose. In this lesson, we will learn about how we can use that can be used to update documents in Mongoose.

Much like in when finding documents, we can use operators to documents in Mongoose, too!

Special types of Updates

Operators are used to perform special types of updates on a document. In the previous lesson, you may have noticed that all of our updates were essentially overwriting values in the document. This is fine for simple updates, but what if we want to perform more complex updates?

Using operators, we can perform complex updates like:

  • Incrementing a value
  • Adding an element to an array
  • Removing an element from an array
  • Adding an element to an array only if it doesn't already exist

There are a ton of operators available in Mongoose that allow us to perform these complex updates. Here we'll be covering some of the most common ones, but for a more comprehensive list, you can refer to the MongoDB.

Using Operators

Operators are used in the same way as the update values in the updateOne and updateMany methods. The only difference is that the value is an object that contains the operator as the key and the value as the value.

Here is an example of using the $inc operator to increment a value:

//
const result = await User.updateOne(
  { name: "Alice" },
  // instead of passing in a value, we pass in an object with the operator as the key
  { $inc: { age: 1 } },
);

In this example, we are incrementing the age field of the document with the name "Alice" by 1.

This way, we don't even need to know the current value of the age field. The $inc operator will take care of incrementing the value for us. This can be useful for updating counters, views, or any other value that needs to be incremented- and saves us the trouble of fetching the document first.

Array Operators

Mongoose also provides operators for updating arrays. Here are some of the most commonly used array operators:

$push

Used to add an element to an array.

//
{
  "name": "Alice",
  "hobbies": ["Reading"]
}
//
const result = await User.updateOne(
  { name: "Alice" },
  { $push: { hobbies: "Hiking" } },
);
//
{
  "name": "Alice",
  "hobbies": ["Reading", "Hiking"]
}

$pull

Used to remove an element from an array.

//
{
  "name": "Alice",
  "hobbies": ["Reading", "Hiking"]
}
//
const result = await User.updateOne(
  { name: "Alice" },
  { $pull: { hobbies: "Hiking" } },
);
//
{
  "name": "Alice",
  "hobbies": ["Reading"]
}

$addToSet

Used to add an element to an array only if it doesn't already exist.

//
{
  "name": "Alice",
  "hobbies": ["Reading"]
}
//
const result = await User.updateOne(
  { name: "Alice" },
  { $addToSet: { hobbies: "Reading" } },
);
//
{
  "name": "Alice",
  "hobbies": ["Reading"]
}

$Each

Used to add multiple elements to an array.

//
{
  "name": "Alice",
  "hobbies": ["Reading"]
}
//
const result = await User.updateOne(
  { name: "Alice" },
  { $push: { hobbies: { $each: ["Hiking", "Cooking"] } } },
);
//
{
  "name": "Alice",
  "hobbies": ["Reading", "Hiking", "Cooking"]
}

Conclusion

Operators are a powerful way to perform complex updates on documents in Mongoose. They allow us to perform operations like incrementing values and complex array updates without having to fetch the document first.

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Deleting Documents