Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sourabhbagrecha/828a6a2deab464cd675b239b4b7211d3 to your computer and use it in GitHub Desktop.
Save sourabhbagrecha/828a6a2deab464cd675b239b4b7211d3 to your computer and use it in GitHub Desktop.
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