Skip to content

Doc improvements #4694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/main/antora/modules/ROOT/pages/mongodb/mapping/mapping.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ Public `JavaBean` properties are not used.
[[mapping.conventions.id-field]]
=== How the `_id` field is handled in the mapping layer.

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.
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.

The following outlines what field will be mapped to the `_id` document field:

* A field annotated with `@Id` (`org.springframework.data.annotation.Id`) will be mapped to the `_id` field.
* A field annotated with `@Id` (`org.springframework.data.annotation.Id`) will be mapped to the `_id` field. +
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`.
* A field without an annotation but named `id` will be mapped to the `_id` field.
* The default field name for identifiers is `_id` and can be customized via the `@Field` annotation.

[cols="1,2",options="header"]
.Examples for the translation of `_id` field definitions
Expand All @@ -54,23 +54,23 @@ The following outlines what field will be mapped to the `_id` document field:
| `@Id` `String` x
| `_id`

| `@Field("x")` `@Id` `String` x
| `_id`
| `@Field("x")` `@Id` `String` y
| `x`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect, @Id takes precedence. I will update the change during the merge.

|===

The following outlines what type conversion, if any, will be done on the property mapped to the _id document field.

* 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.
ObjectId as a field type is also valid.
If you specify a value for `id` in your application, the conversion to an ObjectId is detected to the MongoDB driver.
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.
If you specify a value for `id` in your application, the conversion to an ObjectId is done by the MongoDB driver.
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.
This also applies if the field is annotated with `@Id`.
* If a field is annotated with `@MongoId` in the Java class it will be converted to and stored as using its actual type.
No further conversion happens unless `@MongoId` declares a desired field type.
If no value is provided for the `id` field, a new `ObjectId` will be created and converted to the properties type.
* If a field is annotated with `@MongoId(FieldType.…)` in the Java class it will be attempted to convert the value to the declared `FieldType`.
If no value is provided for the `id` field, a new `ObjectId` will be created and converted to the declared type.
* 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.
* 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.
* 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.

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.
Expand Down