MongoDB $add Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $add?

$add is a MongoDB aggregation expression that returns the sum of two or more numbers. Pass operands in an array. You can also add a number of milliseconds to a Date.

Remember
$add = + for aggregation

{ $add: [ <expr1>, <expr2>, ... ] }

numbers → sum of all values
date + number → date plus that many milliseconds

null / missing → null
needs ≥ 2 expressions in an array
UsePrefer
Sum field values or literals$add
Difference (a − b)$subtract
Product of values$multiply

How to Use $add in MongoDB

Pick the values to sum, wrap them in an array, and emit the result in an expression stage.

  1. Choose an expression stage ($addFields or $project).
  2. List at least two expressions: fields ("$price"), literals (10), or nested math.
  3. Pass that array to $add to get the sum.
  4. For dates, keep one operand a Date and the other a millisecond number.
PieceRequired?Role
$addYesSum of operands
Array of expressionsYes (≥ 2)Numbers, fields, nested ops, or Date + ms
$ifNullNoDefault missing/null values before adding

Pseudocode

Pseudocode
for each document:
    values = evaluate each expression in the array
    if any value is null or missing:
        result = null
    else if one value is a Date and others are numbers:
        result = date + sum(milliseconds)
    else:
        result = sum(values)
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while writing addition expressions.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $add: [ <a>, <b>, ... ] }
Minimum argsTwo expressions in the array
Null ruleAny null/missing → result is null
Date pattern{ $add: [ "$createdAt", 86400000 ] } → +1 day
Order total{ $add: [ "$price", "$tax", "$shipping" ] }
Sample check40 + 5 + 10 = 55

Live Preview

Watch $add sum price, tax, and shipping—the same idea as the mongosh examples below.

Simulates { $add: [ price, tax, shipping ] }.

Result
price = 40, tax = 5, shipping = 10
$add → 55

{ total: 55 }

Worked Walkthrough — Order Total

One order: price 40, tax 5, shipping 10. We want the grand total in one field.

Calculation
Document:
  { price: 40, tax: 5, shipping: 10 }

subtotal with tax = price + tax
                  = 40 + 5
                  = 45

grand total = price + tax + shipping
            = 40 + 5 + 10
            = 55

$add([40, 5, 10]) → 55 ✓

MongoDB Examples

We use one simple order. First we sum price and tax, then add shipping too. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine one order with price 40, tax 5, and shipping 10. We save those three amounts as one document in a collection named orders.

mongosh
db.orders.insertOne({
  _id: 1,
  item: "Notebook",
  price: 40,
  tax: 5,
  shipping: 10
});

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps one document in orders. Run find() to see it:

mongosh
db.orders.find()

Read that document like this:

Field in MongoDBValuePlain English
_id1Document id (row number for this lesson)
item"Notebook"What was ordered
price40Base price
tax5Tax amount
shipping10Shipping fee
Picture
Order bill (easy numbers):

  Notebook
  --------
  price      40
  tax         5
  shipping   10
  --------
  We will compute:
    with_tax = price + tax       → 45
    total    = price + tax + ship → 55

How It Works

  1. Insert once. insertOne adds a single order document to orders.
  2. MongoDB stores fields as written. price, tax, and shipping are just numbers: 40, 5, and 10.
  3. find() shows the same shape. What you inserted is what you get back—ready for $add in the next examples.

Example 2 — Add Two Fields

Goal: compute with_tax as price + tax.

mongosh
db.orders.aggregate([
  {
    $addFields: {
      with_tax: {
        $add: ["$price", "$tax"]
      }
    }
  }
]);

How It Works

  1. Read the stored amounts. MongoDB already has price: 40 and tax: 5 from Example 1.
  2. Add the two fields. $add: ["$price", "$tax"] → 40 + 5 = 45.
  3. Keep all fields. $addFields adds with_tax next to the original order—nothing is deleted.

Example 3 — Add Three Values

Same order, but sum price, tax, and shipping for the grand total.

mongosh
db.orders.aggregate([
  {
    $addFields: {
      total: {
        $add: ["$price", "$tax", "$shipping"]
      }
    }
  }
]);

How It Works

  1. Same stored document. Still using price 40, tax 5, and shipping 10 from Example 1.
  2. Sum three expressions. $add adds every value in the array: 40 + 5 + 10 = 55.
  3. Easy check. Price with tax was 45; plus shipping 10 makes 55—so the total looks right.

Edge Cases & Pitfalls

Check these before using $add in production pipelines.

array

Always pass an array

Unlike unary ops such as $abs, $add needs [ a, b, ... ]. A bare single value is invalid.

null

Null and missing become null

One missing field makes the whole sum null. Use $ifNull when zero is a better default.

date

Numbers mean milliseconds

Date + number adds milliseconds, not days. One day is 86400000. Prefer $dateAdd for calendar units.

vs sum

$add ≠ group $sum

$add sums expressions on one document. Across many documents, use the $sum accumulator.

Performance Notes

ApproachCostBest for
$add of fieldsPer-document mathTotals already on each doc
Many nested addsStill O(docs)Multi-step line-item math
Group with $sumAggregation across docsTotals over many rows

Addition is cheap compared with large sorts or joins. Filter early with $match when only some documents need a computed total.

Key Takeaways

  • Role: sum two or more expressions on one document.
  • Syntax: always an array with at least two values.
  • Dates: number operands are milliseconds.
  • Nulls: any null/missing → result is null.

One line: $add is pipeline +—sum numbers (or add milliseconds to a date) with an array of expressions.

Frequently Asked Questions

$add sums two or more numeric expressions in an aggregation pipeline. It can also add a number of milliseconds to a Date. Use it inside expression stages such as $project and $addFields.
Use { $add: [ <expression1>, <expression2>, ... ] }. Pass an array with at least two expressions—field paths, literals, or nested expressions.
Yes. $add accepts two or more expressions and returns the sum of all of them.
Yes. When one operand is a Date and another is a number, MongoDB treats the number as milliseconds and adds it to the date.
If any argument is null or missing, $add returns null. Use $ifNull to supply a default before adding.
Yes for numbers. Think of $add as MongoDB’s aggregation version of +—run it in the pipeline, not as a standalone find() filter.

Next: $allElementsTrue

Check whether every element in an array evaluates to true.

$allElementsTrue tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful