diff --git a/pom.xml b/pom.xml index f86d31444c..459cdc18ef 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 4.2.0-SNAPSHOT + 4.2.x-4536-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-benchmarks/pom.xml b/spring-data-mongodb-benchmarks/pom.xml index 2de4b6b635..01a703cc00 100644 --- a/spring-data-mongodb-benchmarks/pom.xml +++ b/spring-data-mongodb-benchmarks/pom.xml @@ -7,7 +7,7 @@ org.springframework.data spring-data-mongodb-parent - 4.2.0-SNAPSHOT + 4.2.x-4536-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 41b81f9aa6..02dd10f99c 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -15,7 +15,7 @@ org.springframework.data spring-data-mongodb-parent - 4.2.0-SNAPSHOT + 4.2.x-4536-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index d7a9ddaa63..b7c5995ca5 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 4.2.0-SNAPSHOT + 4.2.x-4536-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java index 73edc2222a..1ddfa88536 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java @@ -945,6 +945,9 @@ default List findDistinct(Query query, String field, String collection, C /** * Triggers findAndModify * to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query}. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an * optional fields specification. Must not be {@literal null}. @@ -961,6 +964,9 @@ default List findDistinct(Query query, String field, String collection, C /** * Triggers findAndModify * to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query}. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an * optional fields specification. Must not be {@literal null}. @@ -979,6 +985,9 @@ default List findDistinct(Query query, String field, String collection, C * Triggers findAndModify * to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query} taking * {@link FindAndModifyOptions} into account. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an * optional fields specification. @@ -999,6 +1008,9 @@ default List findDistinct(Query query, String field, String collection, C * Triggers findAndModify * to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query} taking * {@link FindAndModifyOptions} into account. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an * optional fields specification. Must not be {@literal null}. @@ -1391,8 +1403,11 @@ default long exactCount(Query query, String collectionName) { * leverages Type Conversion API. See * Spring's * Type Conversion" for more details.
- * Insert is used to initially store the object into the database. To update an existing object use the save method. - *
+ * Insert is used to initially store the object into the database. To update an existing object use the + * {@link #save(Object)} method. + *

+ * Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization. + *

* The {@code objectToSave} must not be collection-like. * * @param objectToSave the object to store in the collection. Must not be {@literal null}. @@ -1408,7 +1423,9 @@ default long exactCount(Query query, String collectionName) { * The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. Unless * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
* Insert is used to initially store the object into the database. To update an existing object use the save method. - *
+ *

+ * Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization. + *

* The {@code objectToSave} must not be collection-like. * * @param objectToSave the object to store in the collection. Must not be {@literal null}. @@ -1420,6 +1437,11 @@ default long exactCount(Query query, String collectionName) { /** * Insert a Collection of objects into a collection in a single batch write to the database. + *

+ * If an object within the batch has an {@literal Id} property which holds a {@literal null} value, it will be set + * with the generated Id from MongoDB. + *

+ * Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization. * * @param batchToSave the batch of objects to save. Must not be {@literal null}. * @param entityClass class that determines the collection to use. Must not be {@literal null}. @@ -1431,6 +1453,11 @@ default long exactCount(Query query, String collectionName) { /** * Insert a batch of objects into the specified collection in a single batch write to the database. + *

+ * If an object within the batch has an {@literal Id} property which holds a {@literal null} value, it will be set + * with the generated Id from MongoDB. + *

+ * Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization. * * @param batchToSave the list of objects to save. Must not be {@literal null}. * @param collectionName name of the collection to store the object in. Must not be {@literal null}. @@ -1441,6 +1468,11 @@ default long exactCount(Query query, String collectionName) { /** * Insert a mixed Collection of objects into a database collection determining the collection name to use based on the * class. + *

+ * If an object within the batch has an {@literal Id} property which holds a {@literal null} value, it will be set + * with the generated Id from MongoDB. + *

+ * Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization. * * @param objectsToSave the list of objects to save. Must not be {@literal null}. * @return the inserted objects. @@ -1458,7 +1490,11 @@ default long exactCount(Query query, String collectionName) { * String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your * property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See * Spring's - * Type Conversion" for more details.
+ * Type Conversion" for more details. + *

+ * A potential {@link org.springframework.data.annotation.Version} the property will be auto incremented. The + * operation raises an error in case the document has been modified in between. + *

* The {@code objectToSave} must not be collection-like. * * @param objectToSave the object to store in the collection. Must not be {@literal null}. @@ -1466,6 +1502,8 @@ default long exactCount(Query query, String collectionName) { * @throws IllegalArgumentException in case the {@code objectToSave} is collection-like. * @throws org.springframework.data.mapping.MappingException if the target collection name cannot be * {@link #getCollectionName(Class) derived} from the given object type. + * @throws org.springframework.dao.OptimisticLockingFailureException in case of version mismatch in case a + * {@link org.springframework.data.annotation.Version} is defined. */ T save(T objectToSave); @@ -1478,19 +1516,29 @@ default long exactCount(Query query, String collectionName) { * String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your * property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See * Spring's Type - * Conversion for more details.
+ * Conversion for more details. + *

+ * A potential {@link org.springframework.data.annotation.Version} the property will be auto incremented. The + * operation raises an error in case the document has been modified in between. + *

* The {@code objectToSave} must not be collection-like. * * @param objectToSave the object to store in the collection. Must not be {@literal null}. * @param collectionName name of the collection to store the object in. Must not be {@literal null}. * @return the saved object. * @throws IllegalArgumentException in case the {@code objectToSave} is collection-like. + * @throws org.springframework.dao.OptimisticLockingFailureException in case of version mismatch in case a + * {@link org.springframework.data.annotation.Version} is defined. */ T save(T objectToSave, String collectionName); /** * Performs an upsert. If no document is found that matches the query, a new document is created and inserted by - * combining the query document and the update document.
+ * combining the query document and the update document. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. + *

* NOTE: {@link Query#getSortObject() sorting} is not supported by {@code db.collection.updateOne}. * Use {@link #findAndModify(Query, UpdateDefinition, FindAndModifyOptions, Class, String)} instead. * @@ -1532,6 +1580,9 @@ default long exactCount(Query query, String collectionName) { /** * Performs an upsert. If no document is found that matches the query, a new document is created and inserted by * combining the query document and the update document. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the query document that specifies the criteria used to select a document to be upserted. Must not be * {@literal null}. @@ -1549,6 +1600,9 @@ default long exactCount(Query query, String collectionName) { /** * Updates the first object that is found in the collection of the entity class that matches the query document with * the provided update document. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the query document that specifies the criteria used to select a document to be updated. Must not be * {@literal null}. @@ -1587,7 +1641,10 @@ default long exactCount(Query query, String collectionName) { /** * Updates the first object that is found in the specified collection that matches the query document criteria with - * the provided updated document.
+ * the provided updated document. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the query document that specifies the criteria used to select a document to be updated. Must not be * {@literal null}. @@ -1605,6 +1662,9 @@ default long exactCount(Query query, String collectionName) { /** * Updates all objects that are found in the collection for the entity class that matches the query document criteria * with the provided updated document. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the query document that specifies the criteria used to select a document to be updated. Must not be * {@literal null}. @@ -1642,6 +1702,9 @@ default long exactCount(Query query, String collectionName) { /** * Updates all objects that are found in the collection for the entity class that matches the query document criteria * with the provided updated document. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the query document that specifies the criteria used to select a document to be updated. Must not be * {@literal null}. diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoOperations.java index 84d617e177..4abb5e671e 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoOperations.java @@ -758,6 +758,9 @@ default Flux findDistinct(Query query, String field, String collection, C /** * Triggers findAndModify * to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query}. + *

+ *A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + *incremented if not explicitly specified in the update. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an optional * fields specification. Must not be {@literal null}. @@ -773,6 +776,9 @@ default Flux findDistinct(Query query, String field, String collection, C /** * Triggers findAndModify * to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query}. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an optional * fields specification. Must not be {@literal null}. @@ -790,6 +796,9 @@ default Flux findDistinct(Query query, String field, String collection, C * Triggers findAndModify * to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query} taking * {@link FindAndModifyOptions} into account. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an optional * fields specification. @@ -808,6 +817,9 @@ default Flux findDistinct(Query query, String field, String collection, C * Triggers findAndModify * to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query} taking * {@link FindAndModifyOptions} into account. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an optional * fields specification. Must not be {@literal null}. @@ -1190,7 +1202,9 @@ default Mono exactCount(Query query, String collectionName) { * Spring's * Type Conversion" for more details.
* Insert is used to initially store the object into the database. To update an existing object use the save method. - *
+ *

+ * Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization. + *

* The {@code objectToSave} must not be collection-like. * * @param objectToSave the object to store in the collection. Must not be {@literal null}. @@ -1206,7 +1220,9 @@ default Mono exactCount(Query query, String collectionName) { * The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. Unless * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
* Insert is used to initially store the object into the database. To update an existing object use the save method. - *
+ *

+ * Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization. + *

* The {@code objectToSave} must not be collection-like. * * @param objectToSave the object to store in the collection. Must not be {@literal null}. @@ -1218,6 +1234,11 @@ default Mono exactCount(Query query, String collectionName) { /** * Insert a Collection of objects into a collection in a single batch write to the database. + *

+ * If an object within the batch has an {@literal Id} property which holds a {@literal null} value, it will be set + * with the generated Id from MongoDB. + *

+ * Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization. * * @param batchToSave the batch of objects to save. Must not be {@literal null}. * @param entityClass class that determines the collection to use. Must not be {@literal null}. @@ -1229,6 +1250,11 @@ default Mono exactCount(Query query, String collectionName) { /** * Insert a batch of objects into the specified collection in a single batch write to the database. + *

+ * If an object within the batch has an {@literal Id} property which holds a {@literal null} value, it will be set + * with the generated Id from MongoDB. + *

+ * Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization. * * @param batchToSave the list of objects to save. Must not be {@literal null}. * @param collectionName name of the collection to store the object in. Must not be {@literal null}. @@ -1239,6 +1265,11 @@ default Mono exactCount(Query query, String collectionName) { /** * Insert a mixed Collection of objects into a database collection determining the collection name to use based on the * class. + *

+ * If an object within the batch has an {@literal Id} property which holds a {@literal null} value, it will be set + * with the generated Id from MongoDB. + *

+ * Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization. * * @param objectsToSave the list of objects to save. Must not be {@literal null}. * @return the saved objects. @@ -1256,7 +1287,9 @@ default Mono exactCount(Query query, String collectionName) { * Spring's * Type Conversion" for more details.
* Insert is used to initially store the object into the database. To update an existing object use the save method. - * + *

+ * A potential {@link org.springframework.data.annotation.Version} the property will be auto incremented. The + * operation raises an error in case the document has been modified in between. * @param objectToSave the object to store in the collection. Must not be {@literal null}. * @return the inserted objects. */ @@ -1264,6 +1297,11 @@ default Mono exactCount(Query query, String collectionName) { /** * Insert a Collection of objects into a collection in a single batch write to the database. + *

+ * If an object within the batch has an {@literal Id} property which holds a {@literal null} value, it will be set + * with the generated Id from MongoDB. + *

+ * Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization. * * @param batchToSave the publisher which provides objects to save. Must not be {@literal null}. * @param entityClass class that determines the collection to use. Must not be {@literal null}. @@ -1275,6 +1313,11 @@ default Mono exactCount(Query query, String collectionName) { /** * Insert objects into the specified collection in a single batch write to the database. + *

+ * If an object within the batch has an {@literal Id} property which holds a {@literal null} value, it will be set + * with the generated Id from MongoDB. + *

+ * Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization. * * @param batchToSave the publisher which provides objects to save. Must not be {@literal null}. * @param collectionName name of the collection to store the object in. Must not be {@literal null}. @@ -1285,6 +1328,11 @@ default Mono exactCount(Query query, String collectionName) { /** * Insert a mixed Collection of objects into a database collection determining the collection name to use based on the * class. + *

+ * If an object within the batch has an {@literal Id} property which holds a {@literal null} value, it will be set + * with the generated Id from MongoDB. + *

+ * Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization. * * @param objectsToSave the publisher which provides objects to save. Must not be {@literal null}. * @return the inserted objects. @@ -1300,7 +1348,11 @@ default Mono exactCount(Query query, String collectionName) { * String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your * property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See * Spring's - * Type Conversion" for more details.
+ * Type Conversion" for more details. + *

+ * A potential {@link org.springframework.data.annotation.Version} the property will be auto incremented. The + * operation raises an error in case the document has been modified in between. + *

* The {@code objectToSave} must not be collection-like. * * @param objectToSave the object to store in the collection. Must not be {@literal null}. @@ -1308,6 +1360,8 @@ default Mono exactCount(Query query, String collectionName) { * @throws IllegalArgumentException in case the {@code objectToSave} is collection-like. * @throws org.springframework.data.mapping.MappingException if the target collection name cannot be * {@link #getCollectionName(Class) derived} from the given object type. + * @throws org.springframework.dao.OptimisticLockingFailureException in case of version mismatch in case a + * {@link org.springframework.data.annotation.Version} is defined. */ Mono save(T objectToSave); @@ -1321,11 +1375,16 @@ default Mono exactCount(Query query, String collectionName) { * property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See * Spring's Type * Conversion for more details. + *

+ * A potential {@link org.springframework.data.annotation.Version} the property will be auto incremented. The + * operation raises an error in case the document has been modified in between. * * @param objectToSave the object to store in the collection. Must not be {@literal null}. * @param collectionName name of the collection to store the object in. Must not be {@literal null}. * @return the saved object. * @throws IllegalArgumentException in case the {@code objectToSave} is collection-like. + * @throws org.springframework.dao.OptimisticLockingFailureException in case of version mismatch in case a + * {@link org.springframework.data.annotation.Version} is defined. */ Mono save(T objectToSave, String collectionName); @@ -1339,11 +1398,16 @@ default Mono exactCount(Query query, String collectionName) { * property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See * Spring's Type * Conversion for more details. + *

+ * A potential {@link org.springframework.data.annotation.Version} the property will be auto incremented. The + * operation raises an error in case the document has been modified in between. * * @param objectToSave the object to store in the collection. Must not be {@literal null}. * @return the saved object. * @throws org.springframework.data.mapping.MappingException if the target collection name cannot be * {@link #getCollectionName(Class) derived} from the given object type. + * @throws org.springframework.dao.OptimisticLockingFailureException in case of version mismatch in case a + * {@link org.springframework.data.annotation.Version} is defined. */ Mono save(Mono objectToSave); @@ -1357,16 +1421,25 @@ default Mono exactCount(Query query, String collectionName) { * property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See * Spring's Type * Conversion for more details. + *

+ * A potential {@link org.springframework.data.annotation.Version} the property will be auto incremented. The + * operation raises an error in case the document has been modified in between. * * @param objectToSave the object to store in the collReactiveMongoOperationsection. Must not be {@literal null}. * @param collectionName name of the collection to store the object in. Must not be {@literal null}. * @return the saved object. + * @throws org.springframework.dao.OptimisticLockingFailureException in case of version mismatch in case a + * {@link org.springframework.data.annotation.Version} is defined. */ Mono save(Mono objectToSave, String collectionName); /** * Performs an upsert. If no document is found that matches the query, a new document is created and inserted by - * combining the query document and the update document.
+ * combining the query document and the update document. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. + *

* NOTE: {@link Query#getSortObject() sorting} is not supported by {@code db.collection.updateOne}. * Use {@link #findAndModify(Query, UpdateDefinition, Class)} instead. * @@ -1406,6 +1479,9 @@ default Mono exactCount(Query query, String collectionName) { /** * Performs an upsert. If no document is found that matches the query, a new document is created and inserted by * combining the query document and the update document. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the query document that specifies the criteria used to select a document to be upserted. Must not be * {@literal null}. @@ -1422,7 +1498,11 @@ default Mono exactCount(Query query, String collectionName) { /** * Updates the first object that is found in the collection of the entity class that matches the query document with - * the provided update document.
+ * the provided update document. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. + * * NOTE: {@link Query#getSortObject() sorting} is not supported by {@code db.collection.updateOne}. * Use {@link #findAndModify(Query, UpdateDefinition, Class)} instead. * @@ -1463,7 +1543,10 @@ default Mono exactCount(Query query, String collectionName) { /** * Updates the first object that is found in the specified collection that matches the query document criteria with - * the provided updated document.
+ * the provided updated document. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the query document that specifies the criteria used to select a document to be updated. Must not be * {@literal null}. @@ -1481,6 +1564,9 @@ default Mono exactCount(Query query, String collectionName) { /** * Updates all objects that are found in the collection for the entity class that matches the query document criteria * with the provided updated document. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the query document that specifies the criteria used to select a document to be updated. Must not be * {@literal null}. @@ -1518,6 +1604,9 @@ default Mono exactCount(Query query, String collectionName) { /** * Updates all objects that are found in the collection for the entity class that matches the query document criteria * with the provided updated document. + *

+ * A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto + * incremented if not explicitly specified in the update. * * @param query the query document that specifies the criteria used to select a document to be updated. Must not be * {@literal null}. @@ -1533,7 +1622,8 @@ default Mono exactCount(Query query, String collectionName) { Mono updateMulti(Query query, UpdateDefinition update, Class entityClass, String collectionName); /** - * Remove the given object from the collection by id. + * Remove the given object from the collection by {@literal id} and (if applicable) its + * {@link org.springframework.data.annotation.Version}. * * @param object must not be {@literal null}. * @return the {@link DeleteResult} which lets you access the results of the previous delete. @@ -1552,7 +1642,8 @@ default Mono exactCount(Query query, String collectionName) { Mono remove(Object object, String collectionName); /** - * Remove the given object from the collection by id. + * Remove the given object from the collection by {@literal id} and (if applicable) its + * {@link org.springframework.data.annotation.Version}. * * @param objectToRemove must not be {@literal null}. * @return the {@link DeleteResult} which lets you access the results of the previous delete. @@ -1562,7 +1653,8 @@ default Mono exactCount(Query query, String collectionName) { Mono remove(Mono objectToRemove); /** - * Removes the given object from the given collection. + * Removes the given object from the given collection by {@literal id} and (if applicable) its + * {@link org.springframework.data.annotation.Version}. * * @param objectToRemove must not be {@literal null}. * @param collectionName name of the collection where the documents will be removed from, must not be {@literal null} or empty. @@ -1852,7 +1944,6 @@ Flux mapReduce(Query filterQuery, Class domainType, Class resultTyp Flux mapReduce(Query filterQuery, Class domainType, String inputCollectionName, Class resultType, String mapFunction, String reduceFunction, MapReduceOptions options); - /** * Returns the underlying {@link MongoConverter}. * diff --git a/src/main/antora/modules/ROOT/pages/mongodb/template-crud-operations.adoc b/src/main/antora/modules/ROOT/pages/mongodb/template-crud-operations.adoc index a299be02cd..c0271ee7ba 100644 --- a/src/main/antora/modules/ROOT/pages/mongodb/template-crud-operations.adoc +++ b/src/main/antora/modules/ROOT/pages/mongodb/template-crud-operations.adoc @@ -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 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.