Cara menggunakan mongodb subtract two dates

score:1

Accepted answer

try it

const result = await Complaint.aggregate([
            {
                $match: {
                    createdAt: {
                        $gte: ISODate("2021-08-31T18:30:00.000Z"),
                        $lt: ISODate("2021-09-30T18:29:59.999Z"),
                    },
                },
            },
            { $group: { _id: "$status", count: { $sum: 1 } } },
        ]);

or

const result = await Complaint.aggregate([
            {
                $match: {
                    createdAt: {
                        $gte: new Date("2021-08-31T18:30:00.000Z"),
                        $lt: new Date("2021-09-30T18:29:59.999Z"),
                    },
                },
            },
            { $group: { _id: "$status", count: { $sum: 1 } } },
        ]);

More questions with similar tag


As 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:

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 two dates

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

  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.

    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.