$add (aggregation)
On this page
Definition
-
$add
-
Adds numbers together or adds numbers and a date. If one of the arguments is a date,
$add
treats the other arguments as milliseconds to add to the date.The
$add
expression has the following syntax:The arguments can be any valid expression as long as they resolve to either all numbers or to numbers and a date. For more information on expressions, see Expressions.
Examples
The following examples use a sales
collection with the following documents:
Add Numbers
The following aggregation uses the $add
expression in the $project
pipeline to calculate the total cost:
The operation returns the following results:
Perform Addition on a Date
The following aggregation uses the $add
expression to compute the billing_date
by adding 3*24*60*60000
milliseconds (i.e. 3 days) to the date
field :
The operation returns the following results: