Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data MongoDB BigDecimal support

guys I have a doubt related to BigDecimal value support by Spring Data MongoDB, could someone help me with some news about it, if there will be support for this type, or if anyone knows a workaround to supply my needs. That's the deal: I'm working on a project where we use MongoDB as a DB and Spring as framework, we would like to save the fields where we are supposed to get money values in the database as BigDecimal, I've read that mongo only accepts double as a float number, but I don't think this type is going to be useful. Could you guys give me some light about it?

like image 216
Rodrigo Otani Avatar asked Jun 21 '16 16:06

Rodrigo Otani


People also ask

Which one is not a spring data MongoDB annotation?

Id annotation does not belong to spring-data-mongodb module, it belongs to spring data for widely used both for spring-data-jpa and spring-data-mongodb.

How does spring boot store JSON objects in MongoDB?

To store raw json object/array, all you have to do is to declare the type as "Object" in the Pojo and/or DTO level on your server side. The "Object" type will work with Spring Data and MapStruct too. Then on the client side, you can send your json data as a json data.

What is Spring data MongoDB?

Spring Data for MongoDB is part of the umbrella Spring Data project which aims to provide a familiar and consistent Spring-based programming model for new datastores while retaining store-specific features and capabilities.

What is @document annotation in spring boot?

@Document is an annotation provided by Spring data project. It is used to identify a domain object, which is persisted to MongoDB. So you can use it to map a Java class into a collection inside MongoDB. If you don't use Spring Data, you don't need this annotation.


2 Answers

Spring Data MongoDB converts BigDecimal values to String on write and back to when reading. Please have a look at the data mapping and type conversion section of the reference manual.

The default conversion can be modified via @Field(targetType=...) as shown below.

@Field(targetType = DECIMAL128)
private BigDecimal value;
like image 193
Christoph Strobl Avatar answered Nov 16 '22 02:11

Christoph Strobl


You can change the converter of BigDecimal do Decimal128 which is the java representation of NumberDecimal since mongo 3.4:

@Bean
public MongoCustomConversions mongoCustomConversions() {
    return new MongoCustomConversions(Arrays.asList(

        new Converter<BigDecimal, Decimal128>() {

            @Override
            public Decimal128 convert(@NonNull BigDecimal source) {
                return new Decimal128(source);
            }
        },

        new Converter<Decimal128, BigDecimal>() {

            @Override
            public BigDecimal convert(@NonNull Decimal128 source) {
                return source.bigDecimalValue();
            }

        }


    ));

}

Actually since Spring Data 2.0.7.RELEASE the above changes to following:

@Bean
public MongoCustomConversions mongoCustomConversions() {
    return new MongoCustomConversions(Arrays.asList(
        new BigDecimalDecimal128Converter(),
        new Decimal128BigDecimalConverter()
    ));

}

@WritingConverter
private static class BigDecimalDecimal128Converter implements Converter<BigDecimal, Decimal128> {

    @Override
    public Decimal128 convert(@NonNull BigDecimal source) {
        return new Decimal128(source);
    }
}

@ReadingConverter
private static class Decimal128BigDecimalConverter implements Converter<Decimal128, BigDecimal> {

    @Override
    public BigDecimal convert(@NonNull Decimal128 source) {
        return source.bigDecimalValue();
    }

}
like image 44
Lukasz Frankowski Avatar answered Nov 16 '22 03:11

Lukasz Frankowski