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?
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.
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.
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.
@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.
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;
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With