Skip to content
Advertisement

Mongodb aggregate group by day (php)

I have a collection with documents that look like this:

JavaScript

I want to group and count these documents based on the accountCreated date (count per day), but I am stuck with the handling of dates since the date includes time as well. This is what I have, but it returns the count including the time, witch means lots of entries always with 1 as accounts.

JavaScript

Is there a way to rewrite the date to only take day in account and skip the time?

I have found this example but I can´t really get figure out how to adapt it to this example.

Advertisement

Answer

If accountCreated is a date you can do it like this (I’ll use the mongo shell syntax since I’m not familiar with the php driver):

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