- Notifications
You must be signed in to change notification settings - Fork 1.1k
DATAMONGO-1834 - Add Mongo 3.6 support for DateOperators including timezones #539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
cd9736d
to 0afec2d
Compare yinzara pushed a commit to yinzara/spring-data-mongodb that referenced this pull request Mar 20, 2018
…mezones Original pull request: spring-projects#539
0afec2d
to 3cc4122
Compare yinzara pushed a commit to yinzara/spring-data-mongodb that referenced this pull request Mar 20, 2018
…mezones Original pull request: spring-projects#539
3cc4122
to 0afec2d
Compare …mezones Original pull request: spring-projects#539
0afec2d
to b9373ba
Compare christophstrobl pushed a commit that referenced this pull request Mar 26, 2018
…tring, $dateFromParts and $dateToParts including timezones. Original Pull Request: #539
christophstrobl added a commit that referenced this pull request Mar 26, 2018
Remove DateFactory and split up tests. Introduce dedicated Timezone abstraction and update existing factories to apply the timezone if appropriate. Update builders and align code style. Original Pull Request: #539
christophstrobl pushed a commit that referenced this pull request Mar 27, 2018
…tring, $dateFromParts and $dateToParts including timezones. Original Pull Request: #539
christophstrobl added a commit that referenced this pull request Mar 27, 2018
Remove DateFactory and split up tests. Introduce dedicated Timezone abstraction and update existing factories to apply the timezone if appropriate. Update builders and align code style. Original Pull Request: #539
christophstrobl pushed a commit that referenced this pull request Mar 28, 2018
…tring, $dateFromParts and $dateToParts including timezones. Original Pull Request: #539
christophstrobl added a commit that referenced this pull request Mar 28, 2018
Remove DateFactory and split up tests. Introduce dedicated Timezone abstraction and update existing factories to apply the timezone if appropriate. Update builders and align code style. Original Pull Request: #539
christophstrobl pushed a commit that referenced this pull request Mar 28, 2018
Increase visibility of Timezone factory methods. Add missing nullable annotation. Tweaked Javadoc. Add tests for Timezone using expressions/field references. Original Pull Request: #539
christophstrobl pushed a commit that referenced this pull request Apr 3, 2018
…tring, $dateFromParts and $dateToParts including timezones. Original Pull Request: #539
christophstrobl added a commit that referenced this pull request Apr 3, 2018
Remove DateFactory and split up tests. Introduce dedicated Timezone abstraction and update existing factories to apply the timezone if appropriate. Update builders and align code style. Original Pull Request: #539
christophstrobl pushed a commit that referenced this pull request Apr 3, 2018
Increase visibility of Timezone factory methods. Add missing nullable annotation. Tweaked Javadoc. Add tests for Timezone using expressions/field references. Original Pull Request: #539
christophstrobl pushed a commit that referenced this pull request Apr 3, 2018
…tring, $dateFromParts and $dateToParts including timezones. Original Pull Request: #539
christophstrobl added a commit that referenced this pull request Apr 3, 2018
Remove DateFactory and split up tests. Introduce dedicated Timezone abstraction and update existing factories to apply the timezone if appropriate. Update builders and align code style. Original Pull Request: #539
christophstrobl pushed a commit that referenced this pull request Apr 3, 2018
Increase visibility of Timezone factory methods. Add missing nullable annotation. Tweaked Javadoc. Add tests for Timezone using expressions/field references. Original Pull Request: #539
christophstrobl pushed a commit that referenced this pull request Apr 3, 2018
…tring, $dateFromParts and $dateToParts including timezones. Original Pull Request: #539
christophstrobl added a commit that referenced this pull request Apr 3, 2018
Remove DateFactory and split up tests. Introduce dedicated Timezone abstraction and update existing factories to apply the timezone if appropriate. Update builders and align code style. Original Pull Request: #539
christophstrobl pushed a commit that referenced this pull request Apr 3, 2018
Increase visibility of Timezone factory methods. Add missing nullable annotation. Tweaked Javadoc. Add tests for Timezone using expressions/field references. Original Pull Request: #539
thank you @yinzara! That's been merged, slightly polished and back ported. |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
While the issue only requested support for $dateFromString, $dateFromParts and $dateToParts, I actually implemented the entirety of the Mongo 3.6 supported attributes related to dates. I have added extensive test cases in DateOperatorsUnitTests that cover existing methods (as there were not existing test cases for DateOperators) and new methods.
This includes support for specifying "timezone" with nearly every date function available. I created a new DateAggregationExpression abstract base class to handle this case and included warnings in the JavaDocs about pre3.6 incompatibilities.
This required many additional methods being added to support an optional timezone and all aggregation expression classes to be modified.
Mongo does not by default support a "$quarter" operator on a date (to get the business quarter) however it can be easily achieved through conditionals so I added it as a "pseudo AggregationExpression (as it is still an aggregation expression). It's extremely useful in aggregation pipelines that you want to do logic on the business quarter a specific date occurred in (see below). If they ever decide to add it, we'll already have support in the library :)
I thought it was helpful to be able to reference the current date as part of the aggregation framework as it's extremely useful in the group pipeline to create conditionals. This also helps to alleviate differences in how certain date elements are calculated between languages/libraries. I created a new DateFactory interface (that allows for injecting of fixed dates in the case of tests) that defaults to calling "new java.util.Date()" when the query is executed. This change also contemplates this issue which calls for a "$$now" aggregation pipeline variable to be bound to the current date. In the future our DateFactory could produce a binding to that variable.
For instance, if you wanted to sum the 'amount' field of records that's date field was in the current calendar week/quarter/month/year:
group("employee.id")
.count().as("total_sold")
.sum(when(and(
valueOf(dateOf("item_date").isoWeek()).equalTo(currentDate().isoWeek()),
valueOf(dateOf("item_date").isoWeekYear()).equalTo(currentDate().isoWeekYear())
)).thenValueOf("amount").otherwise(0)).as("wtd")
.sum(when(and(
valueOf(dateOf("item_date").month()).equalTo(currentDate().month()),
valueOf(dateOf("item_date").year()).equalTo(currentDate().year())
)).thenValueOf("amount").otherwise(0)).as("mtd")
.sum(when(and(
valueOf(dateOf("item_date").quarter()).equalTo(currentDate().quarter()),
valueOf(dateOf("item_date").year()).equalTo(currentDate().year())
)).thenValueOf("amount").otherwise(0)).as("qtd")
.sum(when(
valueOf(dateOf("item_date").year()).equalTo(currentDate().year())
).thenValueOf("amount").otherwise(0)).as("ytd")