- MongoDB CRUD Operations >
- Update Documents >
- Updates with Aggregation Pipeline
Updates with Aggregation Pipeline¶
Starting in MongoDB 4.2, you can use the aggregation pipeline for update operations. With the update operations, the aggregation pipeline can consist of the following stages:
Using the aggregation pipeline allows for a more expressive update statement, such as expressing conditional updates based on current field values or updating one field using the value of another field(s).
Example 1¶
Tip
You can try out the example in the provided shell. Click inside the shell to connect. Once connected, you can run the examples in the shell.
Create an example students collection (if the collection does
not currently exist, insert operations will create the collection):
To verify, query the collection:
The following db.collection.updateOne() operation uses an
aggregation pipeline to update the document with _id: 3:
Specifically, the pipeline consists of a $set stage
which adds the test3 field (and sets its value to 98) to the
document and sets the modified field to the current datetime.
The operation uses the aggregation variable NOW for the
current datetime. To access the variable, prefix with $$ and enclose
in quotes.
To verify the update, you can query the collection:
Example 2¶
Tip
You can try out the example in the provided shell. Click inside the shell to connect. Once connected, you can run the examples in the shell.
Create an example students2 collection (if the collection does not
currently exist, insert operations will create the collection):
To verify, query the collection:
The following
db.collection.updateMany() operation uses an aggregation
pipeline to standardize the fields for the documents (i.e. documents
in the collection should have the same fields) and update the
modified field:
Specifically, the pipeline consists of:
- a
$replaceRootstage with a$mergeObjectsexpression to set default values for thequiz1,quiz2,test1andtest2fields. The aggregation variableROOTrefers to the current document being modified. To access the variable, prefix with$$and enclose in quotes. The current document fields will override the default values. - a
$setstage to update themodifiedfield to the current datetime. The operation uses the aggregation variableNOWfor the current datetime. To access the variable, prefix with$$and enclose in quotes.
To verify the update, you can query the collection:
Example 3¶
Tip
You can try out the example in the provided shell. Click inside the shell to connect. Once connected, you can run the examples in the shell.
Create an example students3 collection (if the collection does not
currently exist, insert operations will create the collection):
To verify, query the collection:
The following db.collection.updateMany() operation uses an
aggregation pipeline to update the documents with the calculated
grade average and letter grade.
Specifically, the pipeline consists of:
- a
$setstage to calculate the truncated average value of thetestsarray elements and to update themodifiedfield to the current datetime. To calculate the truncated average, the stage uses the$avgand$truncexpressions. The operation uses the aggregation variableNOWfor the current datetime. To access the variable, prefix with$$and enclose in quotes. - a
$setstage to add thegradefield based on theaverageusing the$switchexpression.
To verify the update, you can query the collection:
Example 4¶
Tip
You can try out the example in the provided shell. Click inside the shell to connect. Once connected, you can run the examples in the shell.
Create an example students4 collection (if the collection does
not currently exist, insert operations will create the collection):
To verify, query the collection:
The following db.collection.updateOne() operation uses an
aggregation pipeline to add quiz scores to the document with _id:
2:
To verify the update, query the collection:
Example 5¶
Tip
You can try out the example in the provided shell. Click inside the shell to connect. Once connected, you can run the examples in the shell.
Create an example temperatures collection that contains
temperatures in Celsius (if the collection does not currently exist,
insert operations will create the collection):
To verify, query the collection:
The following db.collection.updateMany() operation uses an
aggregation pipeline to update the documents with the corresponding
temperatures in Fahrenheit:
Specifically, the pipeline consists of an $addFields
stage to add a new array field tempsF that contains the
temperatures in Fahrenheit. To convert each celsius temperature in
the tempsC array to Fahrenheit, the stage uses the
$map expression with $add and
$multiply expressions.
To verify the update, you can query the collection:
Additional Examples¶
See also the various update method pages for additional examples: