$add
The `$add` operator adds numbers together or adds numbers and dates. When adding numbers and dates, the numbers are interpreted as milliseconds.
Syntax
{ $add: [ <listOfExpressions> ] }
Parameters
<listOfExpressions>
stringrequiredAny valid expressions that resolve to numbers or dates. The expressions can be any combination of numbers and dates.
Examples
Sample Data
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"staff": { "totalStaff": { "fullTime": 8, "partTime": 20 } }
}
Get the current and project staff count
Calculate the total staff and project the total staff looking forward using the $add operator.
Query:
db.stores.aggregate([
{ $match: { _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" } },
{
$project: {
name: 1,
currentTotalStaff: { $add: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"] },
projectedNextYearStaff: { $add: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime", 2] }
}
}
])
Output:
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub",
"currentTotalStaff": 39,
"projectedNextYearStaff": 41
}
]