For AI agents: a documentation index is available at https://www.mongodb.com/pt-br/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

$set (aggregation stage)

Note

Disambiguation

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

$set

Adds new fields to documents. $set outputs documents that contain all existing fields from the input documents and the new fields.

The $set stage is an alias for $addFields.

Both stages are equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.

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

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

$set has the following form:

{ $set: { <newField>: <expression>, ... } }

Specify the name of each field to add and set its value to an aggregation expression or an empty object.

Important

If the new field name matches an existing field name (including _id), $set overwrites that field's value with the specified expression.

  • $set appends new fields to existing documents. You can include one or more $set stages in an aggregation operation.

  • You can use $set with embedded objects. You can set a value to an aggregation expression or to an empty object. For example, the following nested objects are accepted:

    {$set: { a: { b: { } } } }

    To add fields to embedded documents (including documents in arrays), use dot notation. See example.

  • To add an element to an existing array field with $set, use $concatArrays. See example.

The examples on this page use data from the sample_mflix 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 operation uses two $set stages to include three new fields in the output documents:

db.movies.aggregate( [
{ $match: { runtime: { $gt: 1000 } } },
{
$set: {
imdbScoreScaled: { $multiply: [ "$imdb.rating", 10 ] },
runtimeHours: {
$floor: { $divide: [ "$runtime", 60 ] }
}
}
},
{
$set: {
totalScore: {
$add: [ "$imdbScoreScaled", "$runtimeHours" ]
}
}
}
] )
[
{ _id: ..., title: 'Baseball', runtime: 1140, imdbScoreScaled: 91, runtimeHours: 19, totalScore: 110 },
{ _id: ..., title: 'Centennial', runtime: 1256, imdbScoreScaled: 85, runtimeHours: 20, totalScore: 105 }
]
...
First Stage ($set):
The first $set stage adds two fields: imdbScoreScaled, which multiplies the IMDB rating by 10, and runtimeHours, which divides the runtime by 60 and rounds down to the nearest integer.
Second Stage ($set):
The second $set stage adds totalScore, which sums imdbScoreScaled and runtimeHours computed in the previous stage.

Use dot notation to add new fields to embedded documents.

The following aggregation operation adds a new field normalizedRating to the embedded document imdb:

db.movies.aggregate( [
{ $match: { runtime: { $gt: 1000 } } },
{
$set: {
"imdb.normalizedRating": {
$multiply: [ "$imdb.rating", 10 ]
}
}
}
] )
[
{ _id: ..., title: 'Baseball', imdb: { '...': '...', normalizedRating: 91 } },
{ _id: ..., title: 'Centennial', imdb: { '...': '...', normalizedRating: 85 } }
]
...

The following $set operation overwrites the rated field:

db.movies.aggregate( [
{ $match: { title: "Baseball" } },
{ $set: { rated: "UNRATED" } }
] )
[
{ _id: ..., title: 'Baseball', rated: 'UNRATED' }
]
...

You can replace one field with another. The following operation replaces _id with the title field value, then sets title to the string "movie":

db.movies.aggregate( [
{ $match: { runtime: { $gt: 1000 } } },
{ $set: { _id: "$title", title: "movie" } }
] )
[
{ _id: 'Baseball', title: 'movie' },
{ _id: 'Centennial', title: 'movie' }
]
...

To add an element to an existing array field, use $concatArrays with $set. The following operation replaces the genres field with a new array that concatenates the current genres array with [ "Classic" ]:

db.movies.aggregate( [
{ $match: { title: "Baseball" } },
{
$set: {
genres: {
$concatArrays: [ "$genres", [ "Classic" ] ]
}
}
}
] )
[
{ _id: ..., title: 'Baseball', genres: [ 'Documentary', 'History', 'Sport', 'Classic' ] }
]
...

The following aggregation operation adds a new field titleWithYear to each document that concatenates the movie title with its release year:

db.movies.aggregate( [
{ $match: { runtime: { $gt: 1000 } } },
{
$set: {
titleWithYear: {
$concat: [
"$title",
" (",
{ $toString: "$year" },
")"
]
}
}
}
] )
[
{ _id: ..., title: 'Baseball', titleWithYear: 'Baseball (1994)' },
{ _id: ..., title: 'Centennial', titleWithYear: 'Centennial (1978)' }
]
...

For related stages, see $addFields.