Skip to content

Conversation

yinzara
Copy link

@yinzara yinzara commented Mar 20, 2018

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")

@yinzara yinzara force-pushed the issue/DATAMONGO-1834 branch from cd9736d to 0afec2d Compare March 20, 2018 09:49
yinzara pushed a commit to yinzara/spring-data-mongodb that referenced this pull request Mar 20, 2018
@yinzara yinzara force-pushed the issue/DATAMONGO-1834 branch from 0afec2d to 3cc4122 Compare March 20, 2018 20:42
yinzara pushed a commit to yinzara/spring-data-mongodb that referenced this pull request Mar 20, 2018
@yinzara yinzara force-pushed the issue/DATAMONGO-1834 branch from 3cc4122 to 0afec2d Compare March 20, 2018 23:56
@yinzara yinzara force-pushed the issue/DATAMONGO-1834 branch from 0afec2d to b9373ba Compare March 21, 2018 00:34
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
@christophstrobl
Copy link
Member

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

Labels

None yet

2 participants