I am a beginner in express and mongoDB. I was working on a task where I have a model called sales of the type -
{
userName : String,
amount : Number,
date : Date
}
Now I have to create an API which should have a param either of 3 types - "daily", "weekly", "monthly". If the param is
daily - then I have to send stats(sum of amount) on the basis of each hour of the day from sales table.
weekly - then I have to send stats on basis of each day of the week
monthly - then I have to send stats on the basis of each day of the month
After thinking of some logic I came up with this -
I was able to do the daily stats but it also looks complex
app.get("/fetchSales/:id",async (req, res)=>{
const { id } = req.params;
const allSales = await sales.find({});
const dates = [];
for(i in allSales){
let date = allSales[i].date.toISOString().replace('Z', '').replace('T', '');
const dateFormat = dayjs(date);
dateFormat.amount = allSales[i].amount;
dates.push(dateFormat);
}
if(id === "daily") {
const stats = {};
for(let hour = 0; hour < 24; hour++) {
let sum = 0
for (x in dates) {
if (dates[x]["$H"] === hour) sum += dates[x].amount
}
stats[hour] = sum;
}
res.send(stats);
}
})
But going forward I have to use several loop and it's not looking good. I have read that maybe aggregate queries could help me.
structure days js gives me is -
d {
'$L': 'en',
'$d': 2021-01-13T13:00:00.000Z,
'$x': {},
'$y': 2021,
'$M': 0,
'$D': 13,
'$W': 3,
'$H': 18,
'$m': 30,
'$s': 0,
'$ms': 0,
amount: 1700 //added on my own//
}
Some output structure that I want - for daily
{
0: 0$,
1: 0$,
2: 24$,
...
23: 2$
}
Any idea how I can deal with this?