I’m building an admin dashboard and i want to count users created in given month or year. I’m using the countDocuments() method with the query param “createdAt” but the result is 0 and i’m expecting a result of 2.
Here is the code:
const currentMonth = new Date().getMonth();
const currentYear = new Date().getFullYear();
const newClientThisMonth = await User.countDocuments({
createdAt: { $eq: currentYear },
});
The “newClientThisYear” variable is returning 0 instead of 2. I know i’m using the countDocuments() method in the wrong way with this field but with other fields like the “role” field everything is working fine.
const clientUsersCount = await User.countDocuments({ role: "user" });
So, how will you do it if it was you ?