As we know, data stored in a mongo database can have special fields, like ObjectId.
Using the spring data framework, when we execute a mongo query (find or aggregate) the data stored in mongodb are mapped to the type of result we want.
plus ObjectId, I have other types that I save in X format, and I read in Y format. for example, I have a "Location" object
{
"latitude": 0.0,
"length": 0.0
}
I store it in mongo in another format (array) so that I can make geolocation requests.
I don't do this manually, but using read and write converters :
@ReadingConverter
class ConverterLocationRead : Converter<List<Double>, Location>
{
override fun convert(source : List<Double>) : Location?
{
return if (source.isNullOrVoid() || source.size < 2)
{
null
}
else
{
val result = Location()
result.latitude = source[1]
result.longitude = source[0]
result
}
}
}
@WritingConverter
class ConverterLocationWrite : Converter<Location, List<Double>>
{
override fun convert(source : Location) : List<Double>
{
return source.toDocument()
}
}
And then i create a bean to indicate for mongo how to map from/to this type
@Bean
fun customConversions() : MongoCustomConversions
{
val converters = ArrayList<Converter<*,*>>()
converters.add(ConverterLocationWrite())
converters.add(ConverterLocationRead())
return MongoCustomConversions(converters)
}
my question is how to do if i want to map a data which is not stored to database?
for example I have a json file containing a list of data that some fields are in the format that needs to be stored to mongo.
how to use the same mongo mapper to transform my data as spring do when i read it directly from the database?