Skip to content

Commit 034c90f

Browse files
mp911dechristophstrobl
authored andcommitted
DATAMONGO-1412 - Document mapping rules for Java types to MongoDB representation.
Original Pull Request: #359 Related pull request: #353 Related ticket: DATAMONGO-1404
1 parent 628e89c commit 034c90f

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed

src/main/asciidoc/reference/mapping.adoc

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,190 @@ The following outlines what type conversion, if any, will be done on the propert
5959

6060
When querying and updating `MongoTemplate` will use the converter to handle conversions of the `Query` and `Update` objects that correspond to the above rules for saving documents so field names and types used in your queries will be able to match what is in your domain classes.
6161

62+
[[mapping-conversion]]
63+
== Data mapping and type conversion
64+
65+
This section explain how types are mapped to a MongoDB representation and vice versa. Spring Data MongoDB supports all types that can be represented as BSON, MongoDB's internal document format.
66+
In addition to these types, Spring Data MongoDB provides a set of built-in converters to map additional types. You can provide your own converters to adjust type conversion, see <<mapping-explicit-converters>> for further details.
67+
68+
[cols="3,1,6", options="header"]
69+
.Type
70+
|===
71+
| Type
72+
| Type conversion
73+
| Sample
74+
75+
| `String`
76+
| native
77+
| `{"firstname" : "Dave"}`
78+
79+
| `double`, `Double`, `float`, `Float`
80+
| native
81+
| `{"weight" : 42.5}`
82+
83+
| `int`, `Integer`, `short`, `Short`
84+
| native +
85+
32-bit integer
86+
| `{"height" : 42}`
87+
88+
| `long`, `Long`
89+
| native +
90+
64-bit integer
91+
| `{"height" : 42}`
92+
93+
| `Date`, `Timestamp`
94+
| native
95+
| `{"date" : ISODate("2019-11-12T23:00:00.809Z")}`
96+
97+
| `byte[]`
98+
| native
99+
| `{"bin" : { "$binary" : "AQIDBA==", "$type" : "00" }}`
100+
101+
| `java.util.UUID` (Legacy UUID)
102+
| native
103+
| `{"uuid" : { "$binary" : "MEaf1CFQ6lSphaa3b9AtlA==", "$type" : "03" }}`
104+
105+
| `Date`
106+
| native
107+
| `{"date" : ISODate("2019-11-12T23:00:00.809Z")}`
108+
109+
| `ObjectId`
110+
| native
111+
| `{"_id" : ObjectId("5707a2690364aba3136ab870")}`
112+
113+
| Array, `List`, `BasicDBList`
114+
| native
115+
| `{"cookies" : [ … ]}`
116+
117+
| `boolean`, `Boolean`
118+
| native
119+
| `{"active" : true}`
120+
121+
| `null`
122+
| native
123+
| `{"value" : null}`
124+
125+
| `DBObject`
126+
| native
127+
| `{"value" : { … }}`
128+
129+
| `AtomicInteger` +
130+
calling `get()` before the actual conversion
131+
| converter +
132+
32-bit integer
133+
| `{"value" : "741" }`
134+
135+
| `AtomicLong` +
136+
calling `get()` before the actual conversion
137+
| converter +
138+
64-bit integer
139+
| `{"value" : "741" }`
140+
141+
| `BigInteger`
142+
| converter +
143+
`String`
144+
| `{"value" : "741" }`
145+
146+
| `BigDecimal`
147+
| converter +
148+
`String`
149+
| `{"value" : "741.99" }`
150+
151+
| `URL`
152+
| converter
153+
| `{"website" : "http://projects.spring.io/spring-data-mongodb/" }`
154+
155+
| `Locale`
156+
| converter
157+
| `{"locale : "en_US" }`
158+
159+
| `char`, `Character`
160+
| converter
161+
| `{"char" : "a" }`
162+
163+
| `NamedMongoScript`
164+
| converter +
165+
`Code`
166+
| `{"_id" : "script name", value: (some javascript code)`}
167+
168+
| `java.util.Currency`
169+
| converter
170+
| `{"currencyCode" : "EUR"}`
171+
172+
| `LocalDate` +
173+
(Joda, Java 8, JSR310-BackPort)
174+
| converter
175+
| `{"date" : ISODate("2019-11-12T00:00:00.000Z")}`
176+
177+
| `LocalDateTime`, `LocalTime`, `Instant` +
178+
(Joda, Java 8, JSR310-BackPort)
179+
| converter
180+
| `{"date" : ISODate("2019-11-12T23:00:00.809Z")}`
181+
182+
| `DateTime` (Joda)
183+
| converter
184+
| `{"date" : ISODate("2019-11-12T23:00:00.809Z")}`
185+
186+
| `DateMidnight` (Joda)
187+
| converter
188+
| `{"date" : ISODate("2019-11-12T00:00:00.000Z")}`
189+
190+
| `ZoneId` (Java 8, JSR310-BackPort)
191+
| converter
192+
| `{"zoneId" : "ECT - Europe/Paris"}`
193+
194+
| `Box`
195+
| converter
196+
| `{"box" : { "first" : { "x" : 1.0 , "y" : 2.0} , "second" : { "x" : 3.0 , "y" : 4.0}}`
197+
198+
| `Polygon`
199+
| converter
200+
| `{"polygon" : { "points" : [ { "x" : 1.0 , "y" : 2.0} , { "x" : 3.0 , "y" : 4.0} , { "x" : 4.0 , "y" : 5.0}]}}`
201+
202+
| `Circle`
203+
| converter
204+
| `{"circle" : { "center" : { "x" : 1.0 , "y" : 2.0} , "radius" : 3.0 , "metric" : "NEUTRAL"}}`
205+
206+
| `Point`
207+
| converter
208+
| `{"point" : { "x" : 1.0 , "y" : 2.0}}`
209+
210+
| `GeoJsonPoint`
211+
| converter
212+
| `{"point" : { "type" : "Point" , "coordinates" : [3.0 , 4.0] }}`
213+
214+
| `GeoJsonMultiPoint`
215+
| converter
216+
| `{"geoJsonLineString" : {"type":"MultiPoint", "coordinates": [ [ 0 , 0 ], [ 0 , 1 ], [ 1 , 1 ] ] }}`
217+
218+
| `Sphere`
219+
| converter
220+
| `{"sphere" : { "center" : { "x" : 1.0 , "y" : 2.0} , "radius" : 3.0 , "metric" : "NEUTRAL"}}`
221+
222+
| `GeoJsonPolygon`
223+
| converter
224+
| `{"polygon" : { "type" : "Polygon", "coordinates" : [[ [ 0 , 0 ], [ 3 , 6 ], [ 6 , 1 ], [ 0 , 0 ] ]] }}`
225+
226+
| `GeoJsonMultiPolygon`
227+
| converter
228+
| `{"geoJsonMultiPolygon" : { "type" : "MultiPolygon", "coordinates" : [
229+
[ [ [ -73.958 , 40.8003 ] , [ -73.9498 , 40.7968 ] ] ],
230+
[ [ [ -73.973 , 40.7648 ] , [ -73.9588 , 40.8003 ] ] ]
231+
] }}`
232+
233+
| `GeoJsonLineString`
234+
| converter
235+
| `{ "geoJsonLineString" : { "type" : "LineString", "coordinates" : [ [ 40 , 5 ], [ 41 , 6 ] ] }}`
236+
237+
| `GeoJsonMultiLineString`
238+
| converter
239+
| `{"geoJsonLineString" : { "type" : "MultiLineString", coordinates: [
240+
[ [ -73.97162 , 40.78205 ], [ -73.96374 , 40.77715 ] ],
241+
[ [ -73.97880 , 40.77247 ], [ -73.97036 , 40.76811 ] ]
242+
] }}`
243+
|===
244+
245+
62246
[[mapping-configuration]]
63247
== Mapping Configuration
64248

0 commit comments

Comments
 (0)