0
0
MongoDBquery~10 mins

$addFields for computed fields in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $addFields for computed fields
Start with original documents
$addFields stage adds new fields
Compute values using expressions
Add computed fields to each document
Output documents with new fields
The $addFields stage takes each document, computes new fields using expressions, and adds these fields to the document.
Execution Sample
MongoDB
db.sales.aggregate([
  { $addFields: { total: { $multiply: ["$price", "$quantity"] } } }
])
This aggregation adds a new field 'total' by multiplying 'price' and 'quantity' in each sales document.
Execution Table
StepInput DocumentExpression EvaluatedComputed Field ValueOutput Document
1{"item": "apple", "price": 2, "quantity": 5}$multiply: [2, 5]10{"item": "apple", "price": 2, "quantity": 5, "total": 10}
2{"item": "banana", "price": 1, "quantity": 10}$multiply: [1, 10]10{"item": "banana", "price": 1, "quantity": 10, "total": 10}
3{"item": "orange", "price": 3, "quantity": 7}$multiply: [3, 7]21{"item": "orange", "price": 3, "quantity": 7, "total": 21}
4No more documentsN/AN/AAggregation ends
💡 All documents processed, aggregation pipeline completes.
Variable Tracker
VariableStartAfter 1After 2After 3Final
pricevaries per doc213N/A
quantityvaries per doc5107N/A
totalundefined101021N/A
Key Moments - 2 Insights
Why does the 'total' field appear in the output documents?
Because $addFields computes the expression (price * quantity) and adds it as the 'total' field to each document, as shown in execution_table rows 1-3.
What happens if a document is missing 'price' or 'quantity'?
The multiplication expression would result in null or error, so the 'total' field might be missing or null. This is not shown here but important to handle in real cases.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the 'total' value for the second document?
A1
B5
C10
D15
💡 Hint
Check execution_table row 2 under 'Computed Field Value'
At which step does the aggregation pipeline finish processing documents?
AStep 4
BStep 3
CStep 2
DStep 1
💡 Hint
Look at execution_table row 4 where it says 'No more documents'
If the 'quantity' was 0 in a document, what would be the 'total' value?
Anull
B0
Cundefined
D1
💡 Hint
Recall multiplication with zero results in zero, as shown in the expression evaluation column
Concept Snapshot
$addFields adds new fields to documents in aggregation.
Syntax: { $addFields: { newField: <expression> } }
Computes values per document and adds them.
Does not remove existing fields.
Useful for calculated fields like totals or flags.
Full Transcript
The $addFields stage in MongoDB aggregation takes each input document and adds new fields by computing expressions. For example, multiplying price and quantity to get a total. Each document is processed one by one, the expression is evaluated, and the new field is added. This process repeats until all documents are processed. The output documents have all original fields plus the new computed fields. This is useful to enrich data without changing original fields. If a field used in the expression is missing, the result may be null or missing. The execution table shows each step with input document, expression, computed value, and output document. The variable tracker shows how price, quantity, and total change per document. Understanding this helps create dynamic fields in aggregation pipelines.