Updating Documents

Explore different update operators ($set, $inc, $push, $pull, etc.) and methods to modify existing documents in a collection.


MongoDB Essentials: The $set Operator

What is the $set Operator?

In MongoDB, the $set operator is an update operator used to modify the value of an existing field in a document or add a new field to a document if it doesn't already exist. It's a fundamental operator for updating data and is widely used in various update operations.

The $set operator takes a document as its operand, where each field in the document specifies a field to be updated or added in the target document. The value associated with each field represents the new value for that field.

How to Use the $set Operator

The basic syntax for using $set within an update operation is:

 db.collection.updateOne(
  { <query> },
  {
    $set: {
      <field1>: <value1>,
      <field2>: <value2>,
      ...
    }
  }
) 
  • db.collection.updateOne(): This is an example of using updateOne(). You can also use other update methods like updateMany(), update() or replaceOne().
  • <query>: This is the query document that selects the documents to be updated.
  • $set: The update operator that modifies the documents.
  • <field1>: <value1>: The field to be updated or added, along with its new value. You can specify multiple fields within the $set operator.

Examples

Let's say we have a collection named products with the following document:

 {
  "_id": ObjectId("64ff1234567890abcdef1234"),
  "name": "Laptop",
  "price": 999,
  "category": "Electronics"
} 

Example 1: Updating an Existing Field

To update the price of the laptop to 1099, we would use the following query:

 db.products.updateOne(
  { "_id": ObjectId("64ff1234567890abcdef1234") },
  {
    $set: {
      "price": 1099
    }
  }
) 

After the update, the document would look like this:

 {
  "_id": ObjectId("64ff1234567890abcdef1234"),
  "name": "Laptop",
  "price": 1099,
  "category": "Electronics"
} 

Example 2: Adding a New Field

To add a new field called description to the laptop document, we would use the following query:

 db.products.updateOne(
  { "_id": ObjectId("64ff1234567890abcdef1234") },
  {
    $set: {
      "description": "A powerful laptop for work and play."
    }
  }
) 

After the update, the document would look like this:

 {
  "_id": ObjectId("64ff1234567890abcdef1234"),
  "name": "Laptop",
  "price": 1099,
  "category": "Electronics",
  "description": "A powerful laptop for work and play."
} 

Example 3: Updating Multiple Fields

You can update multiple fields simultaneously using a single $set operator:

 db.products.updateOne(
  { "_id": ObjectId("64ff1234567890abcdef1234") },
  {
    $set: {
      "price": 1199,
      "discount": 0.1,
      "available": true
    }
  }
) 

This example updates the price, adds a new field called discount, and adds another field called available. After the update, the document would look like this (assuming the document previously only contained _id, name, price, category and description):

 {
  "_id": ObjectId("64ff1234567890abcdef1234"),
  "name": "Laptop",
  "price": 1199,
  "category": "Electronics",
  "description": "A powerful laptop for work and play.",
  "discount": 0.1,
  "available": true
} 

Key Takeaways

  • The $set operator is used to update existing fields or add new fields.
  • It takes a document as its operand, specifying the fields to be updated or added and their new values.
  • It can be used with update methods like updateOne(), updateMany() and update().
  • It's an essential operator for modifying data in MongoDB.