[SERVER-11118] Add dateToString aggregation operator Created: 10/Oct/13  Updated: 08/May/15  Resolved: 24/Jul/14

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 2.7.4

Type: New Feature Priority: Major - P3
Reporter: Alon Horev Assignee: Adam Midvidy
Resolution: Done Votes: 4
Labels: community-team
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-10651 Aggregation expressions to add/subtra... Closed
related to SERVER-7695 Add variants of $week, $year, $dayOfW... Closed
related to SERVER-9626 Aggregation Framework needs to suppor... Closed
is related to DOCS-3823 Docs for new $dateToString aggregatio... Closed
Tested
Backwards Compatibility: Minor Change
Sprint: Server 2.7.4
Participants:

 Description   

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.



 Comments   
Comment by Githook User [ 07/Oct/14 ]

Author:

{u'username': u'TylerBrock', u'name': u'Tyler Brock', u'email': u'tyler.brock@gmail.com'}

Message: SERVER-11118 add additional $dateToString aggregation tests
Branch: master
https://github.com/mongodb/mongo/commit/59deabf9a55140fa502e637058983d67118e3722

Comment by Githook User [ 24/Jul/14 ]

Author:

{u'username': u'amidvidy', u'name': u'Adam Midvidy', u'email': u'amidvidy@gmail.com'}

Message: SERVER-11118 expect new uassert codes in dateToString jstest

Signed-off-by: Eric Milkie <milkie@10gen.com>
Branch: master
https://github.com/mongodb/mongo/commit/b92932d2c00795932014dcc500d7ec45181c7c82

Comment by Githook User [ 24/Jul/14 ]

Author:

{u'username': u'kaloianm', u'name': u'Kaloian Manassiev', u'email': u'kaloian.manassiev@mongodb.com'}

Message: SERVER-11118 Fix duplicate uassert ids
Branch: master
https://github.com/mongodb/mongo/commit/23007a0af84a44080336bc02d17e8cf4b009b1e2

Comment by Githook User [ 24/Jul/14 ]

Author:

{u'username': u'amidvidy', u'name': u'Adam Midvidy', u'email': u'amidvidy@gmail.com'}

Message: SERVER-11118 add dateToString aggregation operator

Signed-off-by: Matt Kangas <matt.kangas@mongodb.com>
Closes #720
Branch: master
https://github.com/mongodb/mongo/commit/308af33f76d1e15c105f04ea762f8951b47c5168

Comment by Asya Kamsky [ 12/Oct/13 ]

While translating date to string is useful, I think truncating to certain time resolution is a useful functionality by itself - plus it would allow hourly aggregations while maintaining the correct type. Currently it's possible by using date math and subtracting appropriate number of milliseconds from date to truncate it down to an hour (or any preferred) resolution...

Maybe that's a separate operator request though - but it seems like a better method to get to hourly granularity of time than turning into a string.

Comment by Alon Horev [ 10/Oct/13 ]

Perhaps boost's implementation of date_time is portable enough: http://www.boost.org/doc/libs/1_54_0/doc/html/date_time/date_time_io.html#date_time.format_flags
It also support milliseconds.

Comment by Mathias Stearn [ 10/Oct/13 ]

One thing to consider is that strftime is full of system-specific extensions which makes it hard to offer as our interface. Additionally none of the system implementations of strftime include milliseconds although ours would have to since BSON Dates have ms resolution. We will probably have to do a from-scratch implementation of strftime-like functionality after deciding on our desired spec to do this properly.

Comment by Alon Horev [ 10/Oct/13 ]

Pull request: https://github.com/alonho/mongo/tree/SERVER-11118

Generated at Thu Feb 08 03:24:56 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.