$addFields
The $addFields stage in the Amazon DocumentDB aggregation pipeline allows you to add new computed fields to documents. This can be useful for adding derived or transformed data to the documents.
Parameters
-
newField: The name of the new field to add. -
expression: An expression that resolves to the value of the new field.
Example (MongoDB Shell)
The following example demonstrates how to use $addFields to add a new field TotalInventory that calculates the total inventory based on the Inventory.OnHand and Inventory.OrderQnty fields.
Create sample documents
db.example.insertMany([ { "Item": "Spray Paint", "Colors": ["Black", "Red", "Green", "Blue"], "Inventory": { "OnHand": 47, "MinOnHand": 50, "OrderQnty": 36 }, "UnitPrice": 3.99 }, { "Item": "Ruler", "Colors": ["Red", "Green", "Blue", "Clear", "Yellow"], "Inventory": { "OnHand": 47, "MinOnHand": 40 }, "UnitPrice": 0.89 } ]);
Query example
db.example.aggregate([ { $addFields: { TotalInventory: { $add: ["$Inventory.OnHand", "$Inventory.OrderQnty"] } } } ])
Output
[
{
"_id" : ObjectId("5bedafbcf65ff161707de24f"),
"Item" : "Ruler",
"Colors" : [ "Red", "Green", "Blue", "Clear", "Yellow" ],
"Inventory" : {
"OnHand" : 47,
"MinOnHand" : 40
},
"UnitPrice" : 0.89,
"TotalInventory" : 47
},
{
"_id" : ObjectId("5bedafbcf65ff161707de250"),
"Item" : "Spray Paint",
"Colors" : [ "Black", "Red", "Green", "Blue" ],
"Inventory" : {
"OnHand" : 47,
"MinOnHand" : 50,
"OrderQnty" : 36
},
"UnitPrice" : 3.99,
"TotalInventory" : 83
}
]
Code examples
To view a code example for using the $addFields command, choose the tab for the language that you want to use: