Skip to content

Commit ba575f1

Browse files
committed
DATAMONGO-1412 - Document mapping rules for Java types to MongoDB representation.
Related pull request: #353 Related ticket: DATAMONGO-1412
1 parent 987669b commit ba575f1

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed

src/main/asciidoc/reference/mapping.adoc

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,186 @@ 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+
| MongoDB representation
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+
| `Date`
102+
| native
103+
| `{"date" : ISODate("2019-11-12T23:00:00.809Z")}`
104+
105+
| `ObjectId`
106+
| native
107+
| `{"_id" : ObjectId("5707a2690364aba3136ab870")}`
108+
109+
| Array, `List`, `BasicDBList`
110+
| native
111+
| `{"cookies" : [ … ]}`
112+
113+
| `boolean`, `Boolean`
114+
| native
115+
| `{"active" : true}`
116+
117+
| `null`
118+
| native
119+
| `{"value" : null}`
120+
121+
| `DBObject`
122+
| native
123+
| `{"value" : { … }}`
124+
125+
| `AtomicInteger` +
126+
calling `get()` before the actual conversion
127+
| converter +
128+
32-bit integer
129+
| `{"value" : "741" }`
130+
131+
| `AtomicLong` +
132+
calling `get()` before the actual conversion
133+
| converter +
134+
64-bit integer
135+
| `{"value" : "741" }`
136+
137+
| `BigInteger`
138+
| converter +
139+
`String`
140+
| `{"value" : "741" }`
141+
142+
| `BigDecimal`
143+
| converter +
144+
`String`
145+
| `{"value" : "741.99" }`
146+
147+
| `URL`
148+
| converter
149+
| `{"website" : "http://projects.spring.io/spring-data-mongodb/" }`
150+
151+
| `Locale`
152+
| converter
153+
| `{"locale : "en_US" }`
154+
155+
| `char`, `Character`
156+
| converter
157+
| `{"char" : "a" }`
158+
159+
| `NamedMongoScript`
160+
| converter +
161+
`Code`
162+
| `{"_id" : "script name", value: (some javascript code)`}
163+
164+
| `java.util.Currency`
165+
| converter
166+
| `{"currencyCode" : "EUR"}`
167+
168+
| `LocalDate` +
169+
(Joda, Java 8, JSR310-BackPort)
170+
| converter
171+
| `{"date" : ISODate("2019-11-12T00:00:00.000Z")}`
172+
173+
| `LocalDateTime`, `LocalTime`, `Instant` +
174+
(Joda, Java 8, JSR310-BackPort)
175+
| converter
176+
| `{"date" : ISODate("2019-11-12T23:00:00.809Z")}`
177+
178+
| `DateTime` (Joda)
179+
| converter
180+
| `{"date" : ISODate("2019-11-12T23:00:00.809Z")}`
181+
182+
| `DateMidnight` (Joda)
183+
| converter
184+
| `{"date" : ISODate("2019-11-12T00:00:00.000Z")}`
185+
186+
| `ZoneId` (Java 8, JSR310-BackPort)
187+
| converter
188+
| `{"zoneId" : "ECT - Europe/Paris"}`
189+
190+
| `Box`
191+
| converter
192+
| `{"box" : { "first" : { "x" : 1.0 , "y" : 2.0} , "second" : { "x" : 3.0 , "y" : 4.0}}`
193+
194+
| `Polygon`
195+
| converter
196+
| `{"polygon" : { "points" : [ { "x" : 1.0 , "y" : 2.0} , { "x" : 3.0 , "y" : 4.0} , { "x" : 4.0 , "y" : 5.0}]}}`
197+
198+
| `Circle`
199+
| converter
200+
| `{"circle" : { "center" : { "x" : 1.0 , "y" : 2.0} , "radius" : 3.0 , "metric" : "NEUTRAL"}}`
201+
202+
| `Point`
203+
| converter
204+
| `{ "x" : 1.0 , "y" : 2.0}`
205+
206+
| `GeoJsonPoint`
207+
| converter
208+
| `{"point" : { "type" : "Point" , "coordinates" : [3.0 , 4.0] }}`
209+
210+
| `GeoJsonMultiPoint`
211+
| converter
212+
| `{"geoJsonLineString":{"type":"MultiPoint", "coordinates": [ [0,0],[0,1],[1,1] ] }}`
213+
214+
| `Sphere`
215+
| converter
216+
| `{"sphere" : { "center" : { "x" : 1.0 , "y" : 2.0} , "radius" : 3.0 , "metric" : "NEUTRAL"}}`
217+
218+
| `GeoJsonPolygon`
219+
| converter
220+
| `{"polygon" : { "type" : "Polygon", "coordinates" : [[ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ]] }}`
221+
222+
| `GeoJsonMultiPolygon`
223+
| converter
224+
| `{"geoJsonMultiPolygon" : { "type" : "MultiPolygon", "coordinates" : [
225+
[ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], ] ],
226+
[ [ [ -73.9737, 40.7648 ], [ -73.958, 40.8003 ] ] ]
227+
] }}`
228+
229+
| `GeoJsonLineString`
230+
| converter
231+
| `{ "geoJsonLineString" : {"type" : "LineString", "coordinates" : [ [ 40, 5 ], [ 41, 6 ] ]}}`
232+
233+
| `GeoJsonMultiLineString`
234+
| converter
235+
| `{"geoJsonLineString" : {"type": "MultiLineString", coordinates: [
236+
[ [ -73.97162, 40.78205 ], [ -73.96374, 40.77715 ] ],
237+
[ [ -73.97880, 40.77247 ], [ -73.97036, 40.76811 ] ]
238+
] }}`
239+
|===
240+
241+
62242
[[mapping-configuration]]
63243
== Mapping Configuration
64244

0 commit comments

Comments
 (0)