Cara menggunakan mongodb subtract update

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:

Database: GeeksforGeeks

Collection: Employee

Document: four documents that contain the details of the employees in the form of field-value pairs.

Cara menggunakan mongodb subtract update

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.

In MongoDB, you can use the

db.data.aggregate(
   [
     { $project: { 
       _id: 0,
       a: 1, 
       b: 1, 
       result: { 
         $subtract: [ "$a", "$b" ] } } 
         }
   ]
)
5 aggregation pipeline operator to subtract numbers and/or dates.

Specifically,

db.data.aggregate(
   [
     { $project: { 
       _id: 0,
       a: 1, 
       b: 1, 
       result: { 
         $subtract: [ "$a", "$b" ] } } 
         }
   ]
)
5 can do the following three things:

  • Subtract two numbers to return the difference
  • Subtract a number (in milliseconds) from a date and return the resulting date
  • Subtract two dates to return the difference in milliseconds

The

db.data.aggregate(
   [
     { $project: { 
       _id: 0,
       a: 1, 
       b: 1, 
       result: { 
         $subtract: [ "$a", "$b" ] } } 
         }
   ]
)
5 operator accepts exactly two arguments. Passing the wrong number of arguments results in an error.

Example

Return the sum of the "Quantity" field in the "OrderDetails" table:

SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails;

Try it Yourself »


Definition and Usage

The SUM() function calculates the sum of a set of values.

Note: NULL values are ignored.

Syntax

Parameter Values

ParameterDescriptionexpressionRequired. A field or a formula

Technical Details

MongoDB provides a variety of arithmetic expression operators. The $subtract operator is one of those operators. This operator is used to subtract two numbers or dates and returns the difference.

  1. If the argument is the two numbers, the result will come in a number.
  2. If the argument is the two dates, the result will come in milliseconds.
  3. If the argument is a date and a number in milliseconds, the result will come in date.

Syntax of the $subtract operator:

Note: The arguments must be a valid expression, such as a number or a date. In this operator, the second argument is subtracted by the first argument. If you want to subtract a number from a date, the first argument should always be a date.

Examples:

Suppose we have a collection of the students with the following documents.

Example 1: Subtract two numbers using the $subtract operator

In this example, we are going to subtract the 250 from the "start_date" field using the $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

  1. In a worksheet in Tableau, select Analysis > Create Calculated Field.

  2. In the Calculation Editor that opens, give the calculated field a name.

    In this example, the calculated field is called Profit Ratio.

Step 2: Enter a formula

  1. In the Calculation Editor, enter a formula.

    This example uses the following formula:

    SUM([Profit])/SUM([Sales])

    Formulas use a combination of functions, fields, and operators. To learn more about creating formulas in Tableau, see Formatting Calculations in Tableau(Link opens in a new window) and Functions in Tableau(Link opens in a new window).

  2. When finished, click OK.

    The new calculated field is added to the Data pane. If the new field computes quantitative data, it is added to Measures. If it computes qualitative data, it is added to Dimensions.