Chapter 18: MongoDB Aggregation $addFields

1. What does $addFields actually do? (Very clear teacher explanation)

$addFields does exactly one thing very well:

It adds new fields to the existing documents or overwrites existing fields with new calculated values while keeping ALL the original fields unchanged (unless you explicitly overwrite them).

Compare it side-by-side with $project:

Feature $project $addFields Winner for most real cases
Keeps original fields by default No — you must list every field you want Yes — all original fields stay automatically $addFields
Can remove fields Yes (set to 0) No — cannot remove fields $project
Can rename fields Yes (newName: “$oldName”) No — cannot rename (but can copy + remove later) $project
Can create computed fields Yes Yes Tie
Typical use case Final shaping of output / hide many fields Add calculations / flags / dates mid-pipeline

Golden classroom rule (repeat after me):

Use $addFields when you want to enrich documents with new values but still need all (or most) of the original fields later in the pipeline.

Use $project when you are at the very end and want to clean up, hide, or completely reshape the final output.

2. Basic Syntax (Super simple)

JavaScript

3. Hands-on Examples — Using Our Movie Collection

JavaScript

Example 1: Add simple flags & computed values

JavaScript

→ All original fields (genres, year, country, etc.) are still there — we just added new ones.

Example 2: Add fields before $group (very common pattern)

JavaScript

→ We needed those extra fields only for grouping — $addFields made it clean.

Example 3: Overwrite existing field (sometimes useful)

JavaScript

→ Original rating is replaced everywhere downstream.

4. Quick Comparison Table — When to Choose $addFields vs $project

Situation Recommended Stage Why?
You need to add 2–3 calculated fields but keep everything else $addFields No need to list all original fields
Final output — hide 10+ fields, rename, reshape $project Cleaner final shape, can remove _id easily
Mid-pipeline — need new fields for next $group / $sort $addFields Preserve all data for later stages
Want to both add fields and remove some $addFields + $project Use $addFields first → $project at the end
Overwrite existing field with calculation $addFields Simple — just use same field name

5. Mini Exercise — Try Right Now in mongosh!

  1. Add a field isSuperhit = true if rating ≥ 8.3
  2. Add genresCount = size of genres array
  3. Add titleWithYear = “Movie (year)” using $concat
  4. Add profitScore = revenue – (rating * 100) (dummy)
  5. Then $group by isSuperhit and count movies

Understood beta? $addFields is your best friend when you want to enrich documents step-by-step without losing any original information — it’s the stage that makes complex pipelines readable and maintainable.

Next class — what shall we cover?

  • $addFields vs $project deep comparison with same pipeline
  • Using $addFields inside $lookup (joined data enrichment)
  • Date / string / math / conditional operators inside $addFields
  • Or let’s finally build a complete “Movie Analytics Dashboard” pipeline using all stages we learned?

Tell me — class is ready for the next chapter! 🚀❤️

Any part of $addFields still confusing? Ask freely — we’ll do more live examples together 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *