Created
May 24, 2022 19:32
-
-
Save sourabhbagrecha/828a6a2deab464cd675b239b4b7211d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports = async (input) => { | |
const {from, to} = input; | |
const collection = context.services.get('mongodb-atlas').db('expengo').collection("expenses"); | |
const user = context.user.id; | |
// Pipeline to filter relevant expenses as per the date range | |
// and group them by the mode name and calculate the total amount | |
// per mode. | |
const pipeline = [ | |
{ | |
$match: { | |
author: new BSON.ObjectId(user), | |
createdAt: {$gte: new Date(from), $lte: new Date(to)} | |
} | |
}, | |
{ | |
$group: { | |
_id: "$mode", | |
amount: {$sum: "$amount"} | |
} | |
}, | |
{ | |
$project: { | |
mode: "$_id", | |
amount: 1, | |
_id: 0 | |
} | |
} | |
]; | |
const modes = await collection.aggregate(pipeline).toArray(); | |
return { modes }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment