Skip to content
Advertisement

Mongoose how to count unique days in a month?

i am working on a problem where in my database there supposed to be multiple entries from day to day. each entry includes a timestamp. the problem is im supposed to find how many days in each month a data has been entered. ex: if a user sends a message every 2 days in may. my answer would be: “the user used the message function for 15 days in may”. now if the user sends 5 messages every 2 days in may. the answer would be still 15. im only counting the days the user has been using the app.

using this query:

JavaScript

i was able to find all data entries on a specific month. the data may look like something like this:

JavaScript

the desired answer would be 3 unique days in may.

How do i achieve this using mongodb? the simplest answer that come to mind is to use additional queries to group by unique days, but i have no idea how to do that using mongo provided i need to access the date.

Advertisement

Answer

This might solves your problem. it will return distinct messages.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement