My goal is to aggregate the document into per day
, per week
.
document template stored in Firestore.
document = {
iat: new Date() // Wed Aug 26 2020 HH:MM:SS GMT+... (... Time),
bill: PropTypes.number,
}
The expected output is
data = {
labels: [
08/26/2020,
08/27/2020,
...
],
datasets: [
...
data: [
bill1 + bill2,
bill3 + bill4 + bill5,
bill6,
bill7 + bill8
],
],
}
What I've tried. The output is per document.
firebase
.firestore()
.collection("tickets")
.orderBy("exp")
.get()
.then((snapshot) => {
if (!snapshot.empty) {
setData({
labels: snapshot.docs.map((doc) => {
const data = doc.data();
const exp = data.exp ? new Date(data.exp.seconds * 1000 + data.exp.milliseconds).toString().split(" ").slice(1, 5).join(" ") : null;
return exp;
}),
datasets: [
{
data: snapshot.docs.map((doc) => {
const data = doc.data();
const bill = data.bill ? data.bill : null;
return bill;
}),
},
],
});
tl:dr
The problem I saw is, it is easier to merge the iat
key into per day or per hour. While it is hard to merge the bill
key based on the merging of the iat
key.
What I can think about now is solving it with nested logic and I imagine it would be cryptic, heads up if you have simpler solution.
I will back in a hour after I found a solution.
Thank you, much appreciated.