What if you could add new insights to your data instantly, without any manual work?
Why $addFields for computed fields in MongoDB? - Purpose & Use Cases
Imagine you have a big list of sales records in a spreadsheet. You want to add a new column that shows the total price by multiplying quantity and price per item. Doing this by hand for hundreds or thousands of rows is tiring and slow.
Manually calculating and adding new columns in a spreadsheet or text file is error-prone and takes a lot of time. If the data changes, you must redo all calculations. It's easy to make mistakes and hard to keep everything updated.
The $addFields stage in MongoDB lets you add new fields to your data automatically by computing values from existing fields. This means you can create new information on the fly without changing the original data, saving time and avoiding errors.
For each record: total = quantity * price_per_item; add total as new columndb.collection.aggregate([{ $addFields: { total: { $multiply: ["$quantity", "$price_per_item"] } } }])It enables you to enrich your data dynamically with new computed fields, making your queries smarter and your reports more insightful.
A store manager can quickly see total sales per item by multiplying quantity sold by unit price, all within the database query, without extra manual work.
Manual calculations are slow and error-prone.
$addFields computes new fields automatically in queries.
This makes data analysis faster, easier, and more reliable.