-1

I want to save to mongodb an org.bson.Document containing an OffsetDateTime instance with the OffsetDateTime saved as a DATE. It is currently saved as an Object.

I'm using my own custom MongoDatabaseFactory.

This is my test data. I've created a org.bson.Document. Appended to Document these types: java.time.OffsetDateTime, java.time.Instant, and java.util.Date.

Saved to monogdb via a MongoTemplate instance. Both Instant and Date are saved as a DATE in mongo. However OffsetDateTime is being saved as an object. I want it to save as a DATE.

I've searched google, found many questions/answers and sites stating to use CustomConverters.

43544908/usage-of-offsetdatetime-with-spring-boot-and-mongodb-results-in-mappingexception

41127665/zoneddatetime-with-mongodb/

27091418/store-java-8-instant-as-bson-date-using-springdata-mongodb

I've created two converters. OffsetDateTime -> Date and Date -> OffsetDateTime. Annotated with @WritingConverter and @ReadingConverter. Instantiated MongoCustomConversions, added my converters, set conversions on MappingMongoConverter, and passed to MongoTemplate's constructor.

I've tried many permutations setting the conversions on the MongoTemplate. None work. I've debugged this in eclipse but haven't found where the Document is being converted.

Here's what I see in mongodb. Snapshot of mongodb record Any help is appreciated.

Mercer
  • 1
  • 1

1 Answers1

0

To get it working, I also had to add a Document Provider to handle OffsetDateTimes.

https://jira.mongodb.org/browse/JAVA-2829

Mercer
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 03 '22 at 06:36