Skip to content

Conversation

codeconsole
Copy link
Contributor

@codeconsole codeconsole commented Oct 4, 2025

class Person {
    LocalDateTime created
    Date updated
    Instant modified
}

results in inconsistent mongo storage.

  {
    _id: '68e0824c605bbe8f074c52ba',
    created: ISODate('2025-10-04T02:11:24.201Z'),
    updated: ISODate('2025-10-04T02:11:24.201Z'),
    modified: Long('1759543884201'),
    version: Long('0')
  }

Spring Data persists Instant the same way as Date. So for mongodb it is stored as a BSON DateTime (displayed as ISODate(...)). That’s the correct and expected mapping. Grails, however, stores it as a Long.

Aggregation stages or queries that expect date types:
$year, $month, $week, $dayOfWeek
$dateTrunc, $dateAdd, $dateSubtract
$dateToString, $dateFromParts

so none of this works with the current Instant > Long implementation

Similar to Spring Data, the following should be interchangeable without requiring a database update:

    Date created
    LocalDateTime created
    Instant created

Copy link
Contributor

@matrei matrei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Good catch!

However, how will this effect existing applications using grails-data-mongodb?
Should we document that the field type in the database must be updated when people upgrade?

Copy link
Contributor

@jdaugherty jdaugherty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only concern with this is the upgrade guide was not updated to mention this change.

@codeconsole codeconsole requested a review from jdaugherty October 6, 2025 20:25
@codeconsole
Copy link
Contributor Author

codeconsole commented Oct 6, 2025

However, how will this effect existing applications using grails-data-mongodb?

@matrei If anyone out there is actually using Instant, it will thrown an exception. The data must be fixed. Fortunately, there is an easy mongosh solution which I posted in UPGRADE7.md

Should we document that the field type in the database must be updated when people upgrade?

done and a mongosh update example was provided.

@codeconsole codeconsole requested a review from jdaugherty October 7, 2025 18:40
@codeconsole codeconsole merged commit 9434fd1 into apache:7.0.x Oct 8, 2025
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants