Skip to content
Justice Tech School

$match, $project and $addFields

$match filters early, $project chooses or renames fields and $addFields adds computed fields. Put $match first so indexes help.

Example
db.orders.aggregate([
  { $match: { amount: { $gt: 100 } } },
  { $addFields: { tax: { $multiply: ["$amount", 0.075] } } },
  { $project: { amount: 1, tax: 1, _id: 0 } }
])

Exercise

Computed field.

{ $: { tax: 5 } }

Quiz Test

Why put $match first?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.