$add (aggregation)

Definition

  • $add
  • Adds numbers together or adds numbers and a date. If one of thearguments is a date, $add treats the other argumentsas milliseconds to add to the date.

The $add expression has the following syntax:

  1. { $add: [ <expression1>, <expression2>, ... ] }

The arguments can be any valid expression as long as they resolve toeither all numbers or to numbers and a date. For more information onexpressions, see Expressions.

Examples

The following examples use a sales collection with the followingdocuments:

  1. { "_id" : 1, "item" : "abc", "price" : 10, "fee" : 2, date: ISODate("2014-03-01T08:00:00Z") }
  2. { "_id" : 2, "item" : "jkl", "price" : 20, "fee" : 1, date: ISODate("2014-03-01T09:00:00Z") }
  3. { "_id" : 3, "item" : "xyz", "price" : 5, "fee" : 0, date: ISODate("2014-03-15T09:00:00Z") }

Add Numbers

The following aggregation uses the $add expression in the$project pipeline to calculate the total cost:

  1. db.sales.aggregate(
  2. [
  3. { $project: { item: 1, total: { $add: [ "$price", "$fee" ] } } }
  4. ]
  5. )

The operation returns the following results:

  1. { "_id" : 1, "item" : "abc", "total" : 12 }
  2. { "_id" : 2, "item" : "jkl", "total" : 21 }
  3. { "_id" : 3, "item" : "xyz", "total" : 5 }

Perform Addition on a Date

The following aggregation uses the $add expression tocompute the billing_date by adding 32460*60000 milliseconds(i.e. 3 days) to the date field :

  1. db.sales.aggregate(
  2. [
  3. { $project: { item: 1, billing_date: { $add: [ "$date", 3*24*60*60000 ] } } }
  4. ]
  5. )

The operation returns the following results:

  1. { "_id" : 1, "item" : "abc", "billing_date" : ISODate("2014-03-04T08:00:00Z") }
  2. { "_id" : 2, "item" : "jkl", "billing_date" : ISODate("2014-03-04T09:00:00Z") }
  3. { "_id" : 3, "item" : "xyz", "billing_date" : ISODate("2014-03-18T09:00:00Z") }