Update documentation on version property handling.

Original pull request: #4553
Closes #4536
This commit is contained in:
Christoph Strobl
2023-11-13 09:17:08 +01:00
committed by Mark Paluch
parent 5e76c0493e
commit fcf3143b79
3 changed files with 198 additions and 18 deletions

View File

@@ -294,6 +294,12 @@ Server performance of batch and bulk is identical.
However bulk operations do not publish xref:mongodb/lifecycle-events.adoc[lifecycle events].
====
[IMPORTANT]
====
Any `@Version` property that has not been set prior to calling insert will be auto initialized with `1` (in case of a simple type like `int`) or `0` for wrapper types (eg. `Integer`). +
Read more in the see xref:mongodb/template-crud-operations.adoc#mongo-template.optimistic-locking[Optimistic Locking] section.
====
[[mongodb-template-update]]
== Update
@@ -338,6 +344,12 @@ In addition to the `Query` discussed earlier, we provide the update definition b
The `Update` class has methods that match the update modifiers available for MongoDB.
Most methods return the `Update` object to provide a fluent style for the API.
[IMPORTANT]
====
`@Version` properties if not included in the `Update` will be automatically incremented.
Read more in the see xref:mongodb/template-crud-operations.adoc#mongo-template.optimistic-locking[Optimistic Locking] section.
====
[[mongodb-template-update.methods]]
=== Methods for Running Updates for Documents
@@ -478,6 +490,12 @@ Mono<UpdateResult> result = template.update(Person.class)
WARNING: `upsert` does not support ordering. Please use xref:mongodb/template-crud-operations.adoc#mongo-template.find-and-upsert[findAndModify] to apply `Sort`.
[IMPORTANT]
====
`@Version` properties if not included in the `Update` will be automatically initialized.
Read more in the see xref:mongodb/template-crud-operations.adoc#mongo-template.optimistic-locking[Optimistic Locking] section.
====
[[mongo-template.replace]]
=== Replacing Documents in a Collection
@@ -575,6 +593,12 @@ Person upserted = template.update(Person.class)
.findAndModifyValue()
----
[IMPORTANT]
====
`@Version` properties if not included in the `Update` will be automatically incremented.
Read more in the see xref:mongodb/template-crud-operations.adoc#mongo-template.optimistic-locking[Optimistic Locking] section.
====
[[mongo-template.find-and-replace]]
== Find and Replace
@@ -664,6 +688,8 @@ template.save(tmp); // throws OptimisticLockingFailureException
<4> Try to update the previously loaded document that still has `version = 0`. The operation fails with an `OptimisticLockingFailureException`, as the current `version` is `1`.
====
Only certain CRUD operations on `MongoTemplate` do consider and alter version properties. Please consult `MongoOperations` java doc for detailed information.
IMPORTANT: Optimistic Locking requires to set the `WriteConcern` to `ACKNOWLEDGED`. Otherwise `OptimisticLockingFailureException` can be silently swallowed.
NOTE: As of Version 2.2 `MongoOperations` also includes the `@Version` property when removing an entity from the database.