Aggregate in Mongo using only Date Part of a DateTime property
If you need to do an aggregation in MongoDb on a DateTime property and you want to aggregate only by Date part, you can use a simple trick and use a filter to compose the date part before the grouping. Here is the code
|
|
Thanks to the power of aggregation framework, we can construct a pipeline where the first stage create a document with a new property that is composed only by the day-month-year. That new document can have id equal to zero if we only need a count based on that date field, or you can assign original id of the document or whatever else. The subsequent stage of the pipeline is a simple count where the id is the new field that contains only date part of the original DateTime property.
Gian Maria.