Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /

$set (update operator)

Note

Disambiguation

The following page refers to the update operator $set. For the aggregation stage, see $set.

$set

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

You can use $set for deployments hosted in the following environments:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

The $set operator expression has the following form:

{ $set: { <field1>: <value1>, ... } }

To specify a <field> in an embedded document or in an array, use dot notation.

Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. Fields with numeric names are processed in numeric order. See Update Operators Behavior for details.

If the field does not exist, $set adds a new field with the specified value if the new field does not violate a type constraint. If you specify a dotted path for a non-existent field, $set creates the embedded documents as needed to fulfill the dotted path to the field.

If you specify multiple field-value pairs, $set updates or creates each field.

Starting in MongoDB 5.0, mongod no longer raises an error when you use an update operator like $set with an empty operand expression ( { } ). An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).

The $set operator provides the following advantages compared to full document replacement:

  • Targeted Updates: $set modifies only the specified fields, ensuring efficient updates by avoiding unnecessary writes and overhead when you work with large documents.

  • Efficient Oplog Entries: $set optimizes replication by writing only the updated fields to the oplog instead of the entire document. This process reduces the size of oplog entries and allows nodes to replicate changes more efficiently.

  • Simplified Logic: Applications using $set do not need to compute changed fields before they send an update. MongoDB reduces complexity by handling the delta computation internally.

The examples on this page use data from the sample_mflix sample dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the sample dataset. If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.

The following example uses the $set operator to add the label and status fields to the matching movie document:

db.movies.updateOne(
{ title: "The Dark Knight" },
{
$set: {
label: "Award Winner",
status: "classic"
}
}
)

The operation returns the following result:

{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}

To specify a <field> in an embedded document or in an array, use dot notation.

The following example uses dot notation to update the highlight field of the imdb embedded document in the matching movie:

db.movies.updateOne(
{ title: "The Dark Knight" },
{ $set: { "imdb.highlight": "Critics' Choice" } }
)

The operation returns the following result:

{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}

Important

The preceding example uses dot notation to update the highlight field of the embedded imdb document. The following format instead replaces the entire embedded document, removing all other fields from imdb:

db.movies.updateOne(
{ title: "The Dark Knight" },
{ $set: { imdb: { highlight: "Critics' Choice" } } }
)

To specify a <field> in an embedded document or in an array, use dot notation.

The following example uses the $set operator to update the first element (array index 0) of the genres array in the matching movie document:

db.movies.updateOne(
{ title: "The Dark Knight" },
{ $set: { "genres.0": "Thriller" } }
)

The operation returns the following result:

{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}

For additional update operators for arrays, see Array Update Operators.

Tip

Back

$rename

On this page