Hello I am creating a project to show user changes in the PopulationCount data of all the coins according to their categories.
I created a NodeJS script to scrape the data from a coin website, and I have saved it in my MongoDB cloud database as shown below.
In the Front-End, I have created a React App as shown:
When user clicks on a coin category, it should display all the coins from today
Here is the code that relates to showing all the Coin's Categories in my route:
router.get(
"/categories",
asyncHandler(async (req, res) => {
const categories = await Coin.find().distinct(
"category",
(err, results) => {
res.json(results);
}
);
})
);
I am having the biggest problem/trouble with my life because I need to add new property to this document that calculates the difference of the PopulationCount from Today's Date and Yesterday's Date and then adding this new "Trend" property to the document.
So that I can display the Coin data from Today's date and the calculated "trend" property that determines if it there was a decrease or increase in value.
How can I achieve this? So far I have written this code, but I do not know where to go from here.
router.get(
"/categories/:category",
asyncHandler(async (req, res) => {
const { category } = req.params;
const fullName = category.replace(/-/g, " ");
// todays coins
const today = new Date(Date.now());
today.setHours(0, 0, 0, 0);
const todaysCoins = await Coin.find({
category: {
$regex: new RegExp(fullName, "i"),
},
createdAt: {
$gte: today,
},
}).lean();
res.json(todaysCoins);
// loop thru all todays coins
// compare to yesterdays coins
// loop thru array of today and compare to yesterday and add trend
})
// loop through yesterdays coins
const startYest = new Date(Date.now());
startYest.setHours(0, 0, 0, 0);
const oneDayAgo = startYest.getDate() - 1;
startYest.setDate(oneDayAgo);
const endYest = new Date(Date.now());
endYest.setHours(23, 59, 59);
const endYestDayAgo = endYest.getDate() - 1;
endYest.setDate(endYestDayAgo);
const yesterdayCoins = await Coin.find({
category: {
$regex: new RegExp(fullName, "i"),
},
createdAt: {
$gte: startYest,
$lt: endYest,
}
}).lean()
);