-
Type: New Feature
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: Aggregation Framework
-
Minor Change
-
Server 2.7.4
Implement a $dateToString aggregation operator.
e.g. : $dateToString:
{format: "<format string>", date: "<$field>"}
Modifiers should be mostly similar to strftime, but with the addition of milliseconds.
All units should be zero-padded to the maximum number of digits for that unit.
--------
I'd like to propose an aggregation operator that supports formatting date objects as strings (using the strftime function).
This comes very handy when working with time-series data.
For example: if you want to aggregate documents by a specific hour, you'd have to do this: {$group: {_id: {year: {$year: '$date'}, month: {$month: '$date'}, day:
{'$dayOfMonth': '$date'}, hour: {$hour: '$date'}}}}
Here's how you can do it with dateFormat: {$group: {_id: {$dateFormat: ['$date', '%Y-%m-%d %H']}}}
In addition to the shorter syntax, the results look much nicer (-:
I've already implemented the feature and the tests (I'll add a link after the ticket is created).
This is my first contribution to the MongoDB server code. I'll open a pull request referencing this issue. Please tell me if I'm doing anything wrong.
- related to
-
SERVER-10651 Aggregation expressions to add/subtract Years/Quarters/Months
- Closed
-
SERVER-7695 Add variants of $week, $year, $dayOfWeek that conform to the ISO-6801 spec
- Closed
-
SERVER-9626 Aggregation Framework needs to support more date conversion functions like date portion, or time portion of date type
- Closed
- links to