You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoCustomConversions.java
Copy file name to clipboardExpand all lines: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java
Copy file name to clipboardExpand all lines: src/main/antora/modules/ROOT/pages/mongodb/mapping/custom-conversions.adoc
+8-7
Original file line number
Diff line number
Diff line change
@@ -26,15 +26,16 @@ public class Payment {
26
26
----
27
27
{
28
28
"_id" : ObjectId("5ca4a34fa264a01503b36af8"), <1>
29
-
"value" : NumberDecimal(2.099), <2>
30
-
"date" : ISODate("2019-04-03T12:11:01.870Z") <3>
29
+
"value" : NumberDecimal(2.099), <2>
30
+
"date" : ISODate("2019-04-03T12:11:01.870Z") <3>
31
31
}
32
32
----
33
33
<1> String _id_ values that represent a valid `ObjectId` are converted automatically. See xref:mongodb/template-crud-operations.adoc#mongo-template.id-handling[How the `_id` Field is Handled in the Mapping Layer]
34
34
for details.
35
-
<2> The desired target type is explicitly defined as `Decimal128` which translates to `NumberDecimal`. Otherwise the
35
+
<2> The desired target type is explicitly defined as `Decimal128` which translates to `NumberDecimal`.
36
+
Otherwise, the
36
37
`BigDecimal` value would have been truned into a `String`.
37
-
<3> `Date` values are handled by the MongoDB driver itself an are stored as `ISODate`.
38
+
<3> `Date` values are handled by the MongoDB driver itself are stored as `ISODate`.
38
39
====
39
40
40
41
The snippet above is handy for providing simple type hints. To gain more fine-grained control over the mapping process,
@@ -108,6 +109,6 @@ class MyMongoConfiguration extends AbstractMongoClientConfiguration {
108
109
== Big Number Format
109
110
110
111
MongoDB in its early days did not have support for large numeric values such as `BigDecimal`.
111
-
In order to persist values those types got converted into their `String` representation.
112
-
Nowadays `org.bson.types.Decimal128` offers a native solution to storing big numbers.
113
-
Next to influencing the to be stored numeric representation via the `@Field` annotation you can configure `MongoCustomConversions` to use `Decimal128` instead of `String` via the `MongoConverterConfigurationAdapter#numericFormat(...)` or set the `mongo.numeric.format=decimal128` property.
112
+
To persist `BigDecimal` and `BigInteger` values, Spring Data MongoDB converted values their `String` representation.
113
+
With MongoDB Server 3.4, `org.bson.types.Decimal128` offers a native representation for `BigDecimal` and `BigInteger`.
114
+
You can use the to the native representation by either annotating your properties with `@Field(targetType=DECIMAL128)` or by configuring the big decimal representation in `MongoCustomConversions` through `MongoCustomConversions.create(config -> config.bigDecimal(…))`.
0 commit comments