简体   繁体   中英

Spring Data MongoDB Field Converter: Any way to pass a parameter?

When our application is loading a DBOject/Document instance from our MongoDB, we need to convert all of the UTC dates in it into ZoneDateTime values using the zoneIdName, which is within the DBObject/Document instance. We have a lot of Date fields in the DBObject, so I'd like to avoid having to implement a DBOject level Spring Data MongoDB Converter.

Is it possible to use a DBOject field level Spring Data MongoDB Converter, such as the following, which uses a field in the DBObject (ie zoneIdName), to be able to perform the conversion to a ZoneDateTime? If not, I will have to implement an object-level Converter, but it will be tedious...

Thank you for your interest and time.

class ZonedDateTimeReadConverter :
    Converter<Date, ZonedDateTime> {

    override fun convert(date: Date): ZonedDateTime {
        // Need to replace "America/LosAngeles" with 
        // a value from a field in the object being processed.
        return date.toInstant().atZone(ZoneId.of("America/LosAngeles"))
    }
}

seems that converter for whole object is only option

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM