score:1 Accepted answer try it
or
More questions with similar tagAs shown above, this aggregation pipeline command returned the number of days between the date in my document and now (today). In my case, I require an integer number (rounded) so I shall use $trunc command to truncate the value to get an integer. MongoDB provides different types of arithmetic expression operators that are used in the aggregation pipeline stages and an $subtract operator is one of them. This operator is used to subtract two numbers and return the difference in the numbers or to subtract two dates and return the difference in the milliseconds, or subtracts date and number in milliseconds and returns the date. Syntax: { $subtract: [ <expression1>, <expression2> ] } Here, the given arguments must be a valid expression like numbers or a date, and the second argument is subtracted from the first argument. If you are subtracting a number from a date, then the first argument of this operator is a date. Examples: In the following examples, we are working with:
Using $subtract operator subtract two numbers: In this example, we are going to subtract 5*24*60*60000 milliseconds (i.e., 5 days) from the value of projectEndDate field using a $subtract operator. Sometimes your data source does not contain a field (or column) that you need for your analysis. For example, your data source might contain fields with values for Sales and Profit, but not for Profit Ratio. If this is the case, you can create a calculated field for Profit Ratio using data from the Sales and Profit fields. This topic demonstrates how to create a simple calculated field using an example. Step 1: Create the calculated field
Step 2: Enter a formula
|