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: src/main/antora/modules/ROOT/pages/mongodb/mapping/mapping.adoc
+8-8
Original file line number
Diff line number
Diff line change
@@ -28,13 +28,13 @@ Public `JavaBean` properties are not used.
28
28
[[mapping.conventions.id-field]]
29
29
=== How the `_id` field is handled in the mapping layer.
30
30
31
-
MongoDB requires that you have an `_id` field for all documents.If you don't provide one the driver will assign a ObjectId with a generated value.The "_id" field can be of any type the, other than arrays, so long as it is unique.The driver naturally supports all primitive types and Dates.When using the `MappingMongoConverter` there are certain rules that govern how properties from the Java class is mapped to this `_id` field.
31
+
MongoDB requires that you have an `_id` field for all documents.If you don't provide one the driver will assign a ObjectId with a generated value.The `_id` field can be of any type, other than arrays, so long as it is unique.The driver naturally supports all primitive types and Dates.When using the `MappingMongoConverter` there are certain rules that govern how properties from the Java class are mapped to the `_id` field.
32
32
33
33
The following outlines what field will be mapped to the `_id` document field:
34
34
35
-
* A field annotated with `@Id` (`org.springframework.data.annotation.Id`) will be mapped to the `_id` field.
35
+
* A field annotated with `@Id` (`org.springframework.data.annotation.Id`) will be mapped to the `_id` field. +
36
+
Additionally, the name of the document field can be customized via the `@Field` annotation, in which case the document will not contain a field `_id`.
36
37
* A field without an annotation but named `id` will be mapped to the `_id` field.
37
-
* The default field name for identifiers is `_id` and can be customized via the `@Field` annotation.
38
38
39
39
[cols="1,2",options="header"]
40
40
.Examples for the translation of `_id` field definitions
@@ -54,23 +54,23 @@ The following outlines what field will be mapped to the `_id` document field:
54
54
| `@Id` `String` x
55
55
| `_id`
56
56
57
-
| `@Field("x")` `@Id` `String` x
58
-
| `_id`
57
+
| `@Field("x")` `@Id` `String` y
58
+
| `x`
59
59
|===
60
60
61
61
The following outlines what type conversion, if any, will be done on the property mapped to the _id document field.
62
62
63
63
* If a field named `id` is declared as a String or BigInteger in the Java class it will be converted to and stored as an ObjectId if possible.
64
64
ObjectId as a field type is also valid.
65
-
If you specify a value for `id` in your application, the conversion to an ObjectId is detected to the MongoDB driver.
66
-
If the specified `id` value cannot be converted to an ObjectId, then the value will be stored as is in the document's _id field.
65
+
If you specify a value for `id` in your application, the conversion to an ObjectId is done by the MongoDB driver.
66
+
If the specified `id` value cannot be converted to an ObjectId, then the value will be stored as is in the document's `_id` field.
67
67
This also applies if the field is annotated with `@Id`.
68
68
* If a field is annotated with `@MongoId` in the Java class it will be converted to and stored as using its actual type.
69
69
No further conversion happens unless `@MongoId` declares a desired field type.
70
70
If no value is provided for the `id` field, a new `ObjectId` will be created and converted to the properties type.
71
71
* If a field is annotated with `@MongoId(FieldType.…)` in the Java class it will be attempted to convert the value to the declared `FieldType`.
72
72
If no value is provided for the `id` field, a new `ObjectId` will be created and converted to the declared type.
73
-
* If a field named `id` id field is not declared as a String, BigInteger, or ObjectID in the Java class then you should assign it a value in your application so it can be stored 'as-is' in the document's _id field.
73
+
* If a field named `id` is not declared as a String, BigInteger, or ObjectID in the Java class then you should assign it a value in your application so it can be stored 'as-is' in the document's `_id` field.
74
74
* If no field named `id` is present in the Java class then an implicit `_id` file will be generated by the driver but not mapped to a property or field of the Java class.
75
75
76
76
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.
0 commit comments