I have a document in MongoDB which stores monetary data and has the following structure:
{
"_id": 0
},
"data": {
"value": 283360760.75
}
}
As double only provides an approximation of the value, "value" is stored as a Decimal128.
I want to import this document into a JsonNode in my Spring Boot app. For most mongodb data types this works perfectly using the following configuration:
@ReadingConverter
public class DocumentToJsonNodeConverter implements Converter<Document, JsonNode> {
private ObjectMapper objectMapper;
public DocumentToJsonNodeConverter(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@SneakyThrows
public JsonNode convert(Document source) {
return objectMapper.readTree(source.toJson());
}
}
For most data types like strings or doubles this works fine and the value is stored in a TextNode/FloatNode where it can be easily used.
I expected Decimal128 values to be stored in a DecimalNode as BigDecimals, however instead they seem to be stored in a TextNode stored within an ObjectNode.
Can anyone please explain why this behaviour occurs and what I can try to instead use a DecimalNode?