How to count and sum a field between 2 dates?

user3881004

Here is my model file:

income.js

var incomeSchema = new schema({
  issuedBy: { type: schema.Types.ObjectId, ref: 'user' },
  amount: {type: Number, default: 0},
  content: { type: String, default :''},
  note: { type: String, default: ''}
}, {timestamps: true});

Now I want to sum the all values of amount between 2 dates and count and export all the data between these dates into a table.

For example: I want to sum all the amount between Feb 12 2017 and Feb 23 2017 and shows it like this:

1.Number of transactions: 20

2.Total: $2500

3.The table that display all the data between 2 dates.

I have tried some answer using

collection.find( {'createdAt': {$gte: Date, $lte: Date}}

but I still can't do it.

Any solution will be appreciated. Thank you.

s7vr

You can try the below aggregation.

db.collection.aggregate(
    [{
        $match: {
            createdAt: {
                $gte: new Date('2017-02-12'),
                $lte: new Date('2017-02-23')
            }
        }
    }, {
        $group: {
            _id: null,
            Total: {
                $sum: "$amount"
            },
            NoOfTransactions: {
                $sum: 1
            }
        }
    }]
)

You can use $out stage to write to a new collection.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to count fractional days between 2 dates

From Dev

SQLAlchemy sum of a column between 2 dates

From Dev

Count number of days between 2 dates in JPA

From Dev

Total number of months (Count) between 2 dates

From Dev

Count number of including weeks between 2 dates

From Dev

kdb q - count subtable between 2 dates

From Dev

How to count the elapsed nights between two dates?

From Dev

How to count the number of sundays between two dates

From Dev

How to calculate count of records between several dates

From Dev

How to count the number of sundays between two dates

From Dev

mongodb How to make age field in sum not count

From Dev

How to count no. of days between 2 dates from calendar in string format vb

From Dev

Count how many times row type 1 is found between row type 2 dates

From Dev

how can i count the range between 2 dates without counting the weekend in javascript

From Dev

SQL Count between dates

From Dev

SQL sum data between 2 dates in one column

From Dev

Python Pandas Sum Values in Columns If date between 2 dates

From Dev

SQL sum data between 2 dates in one column

From Dev

Calculate number of days between 2 dates, then sum and group by month

From Dev

Wrong count of difference days between 2 dates with joda time?

From Dev

Count occurence of values between 2 dynamic dates in MySQL

From Dev

Count the number of elements between 2 dates conditionally on a variable in R

From Dev

MySQL Count time between 2 dates with multiple rows of data

From Dev

How to Sum total between two given dates in sql server 2008

From Dev

How to sum between dates in a DataTable with multi conditions using LINQ?

From Dev

How to get count of 2nd and 4th Saturday's exactly between 2 dates using sql query

From Dev

how to count days between two dates with where conditions

From Dev

how to count days between two dates with where conditions

From Dev

How to count month and days between two dates through JavaScript?

Related Related

  1. 1

    How to count fractional days between 2 dates

  2. 2

    SQLAlchemy sum of a column between 2 dates

  3. 3

    Count number of days between 2 dates in JPA

  4. 4

    Total number of months (Count) between 2 dates

  5. 5

    Count number of including weeks between 2 dates

  6. 6

    kdb q - count subtable between 2 dates

  7. 7

    How to count the elapsed nights between two dates?

  8. 8

    How to count the number of sundays between two dates

  9. 9

    How to calculate count of records between several dates

  10. 10

    How to count the number of sundays between two dates

  11. 11

    mongodb How to make age field in sum not count

  12. 12

    How to count no. of days between 2 dates from calendar in string format vb

  13. 13

    Count how many times row type 1 is found between row type 2 dates

  14. 14

    how can i count the range between 2 dates without counting the weekend in javascript

  15. 15

    SQL Count between dates

  16. 16

    SQL sum data between 2 dates in one column

  17. 17

    Python Pandas Sum Values in Columns If date between 2 dates

  18. 18

    SQL sum data between 2 dates in one column

  19. 19

    Calculate number of days between 2 dates, then sum and group by month

  20. 20

    Wrong count of difference days between 2 dates with joda time?

  21. 21

    Count occurence of values between 2 dynamic dates in MySQL

  22. 22

    Count the number of elements between 2 dates conditionally on a variable in R

  23. 23

    MySQL Count time between 2 dates with multiple rows of data

  24. 24

    How to Sum total between two given dates in sql server 2008

  25. 25

    How to sum between dates in a DataTable with multi conditions using LINQ?

  26. 26

    How to get count of 2nd and 4th Saturday's exactly between 2 dates using sql query

  27. 27

    how to count days between two dates with where conditions

  28. 28

    how to count days between two dates with where conditions

  29. 29

    How to count month and days between two dates through JavaScript?

HotTag

Archive