How the $set Update Operator Works in MongoDB

The MongoDB $set update operator replaces the value of a field with the specified value.

It’s used in conjunction with update operations, for example when using the update() method to update a document.

Example

Suppose we have a collection called dogs with the following document:

{ "_id" : 1, "name" : "Wag", "weight" : 20 }

And suppose we want to change the dog’s weight. We can run the following update() command to update the weight:

db.dogs.update(
  { _id: 1 },
  {
     $set: { weight: 30 }
  }
)

This will result in the following output:

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Which tells us that one document was matched and modified.

Let’s check the collection:

db.dogs.find()

Result:

{ "_id" : 1, "name" : "Wag", "weight" : 30 }

We can see that the weight has been updated to the specified value.

It’s also possible to increment values by a specified amount, but to do that we would need to use the $inc operator instead of $set.

But this article is all about the $set operator, so let’s continue.

When the Field Doesn’t Exist

If the field that you’re trying to update doesn’t exist, the field will be added to the document with the specified value.

Example:

db.dogs.update(
  { _id: 1 },
  {
     $set: { height: 40 }
  }
)

Output:

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

We can see that one document was matched and modified.

Let’s check the collection again.

db.dogs.find()

Result:

{ "_id" : 1, "name" : "Wag", "weight" : 30, "height" : 40 }

So now the document includes a height field with the specified value.

Embedded Documents

You can update values in embedded documents by using dot notation. If the specified path doesn’t already exist, it is created.

Example:

db.dogs.update(
  { _id: 1 },
  {
     $set: { 
       "meals.breakfast": "Fish", 
       "meals.lunch": "Chicken", 
       "meals.dinner": "Beef" 
       }
  }
)

Output:

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Let’s use findOne() to return the document:

db.dogs.findOne()

Result:

{
	"_id" : 1,
	"name" : "Wag",
	"weight" : 30,
	"height" : 40,
	"meals" : {
		"breakfast" : "Fish",
		"dinner" : "Beef",
		"lunch" : "Chicken"
	}
}

We can see that the embedded document has been added as specified.

Arrays

You can update data in arrays by using dot notation while specifying the index of the element that you want to update.

Suppose we have the following document:

{
	"_id" : 1,
	"name" : "Wag",
	"awards" : [
		"Top Dog",
		"Best Dog",
		"Biggest Dog"
	]
}

Let’s update two of the array elements, and the dog’s name.

db.dogs.update({ 
    _id: 1 
    }, { 
        $set: { 
            "name": "Bark",
            "awards.0": "Bottom Dog", 
            "awards.1": "Worst Dog"
        } 
})

Result:

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 

We can see that one document was matched and modified.

And now look at the document.

db.dogs.findOne()

Result:

{
	"_id" : 1,
	"name" : "Bark",
	"awards" : [
		"Bottom Dog",
		"Worst Dog",
		"Biggest Dog"
	]
}