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.
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.
Comments