diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index 06147d536..2afbcd0fe 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -113,7 +113,23 @@ import com.mongodb.client.result.DeleteResult; import com.mongodb.client.result.UpdateResult; /** - * Primary implementation of {@link MongoOperations}. + * Primary implementation of {@link MongoOperations}. It simplifies the use of imperative MongoDB usage and helps to + * avoid common errors. It executes core MongoDB workflow, leaving application code to provide {@link Document} and + * extract results. This class executes BSON queries or updates, initiating iteration over {@link FindIterable} and + * catching MongoDB exceptions and translating them to the generic, more informative exception hierarchy defined in the + * org.springframework.dao package. Can be used within a service implementation via direct instantiation with a + * {@link MongoDatabaseFactory} reference, or get prepared in an application context and given to services as bean + * reference. + *

+ * Note: The {@link MongoDatabaseFactory} should always be configured as a bean in the application context, in the first + * case given to the service directly, in the second case to the prepared template. + *

{@link ReadPreference} and {@link com.mongodb.ReadConcern}

+ *

+ * {@code ReadPreference} and {@code ReadConcern} are generally considered from {@link Query} and + * {@link AggregationOptions} objects for the action to be executed on a particular {@link MongoCollection}. + *

+ * You can also set the default {@link #setReadPreference(ReadPreference) ReadPreference} on the template level to + * generally apply a {@link ReadPreference}. * * @author Thomas Risberg * @author Graeme Rocher @@ -778,7 +794,7 @@ public class MongoTemplate if (ObjectUtils.isEmpty(query.getSortObject())) { - return doFindOne(createDelegate(query), collectionName, query.getQueryObject(), query.getFieldsObject(), + return doFindOne(collectionName, createDelegate(query), query.getQueryObject(), query.getFieldsObject(), new QueryCursorPreparer(query, entityClass), entityClass); } else { query.limit(1); @@ -827,7 +843,7 @@ public class MongoTemplate Assert.notNull(collectionName, "CollectionName must not be null"); Assert.notNull(entityClass, "EntityClass must not be null"); - return doFind(createDelegate(query), collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass, + return doFind(collectionName, createDelegate(query), query.getQueryObject(), query.getFieldsObject(), entityClass, new QueryCursorPreparer(query, entityClass)); } @@ -847,7 +863,7 @@ public class MongoTemplate String idKey = operations.getIdPropertyName(entityClass); - return doFindOne(CollectionPreparer.identity(), collectionName, new Document(idKey, id), new Document(), + return doFindOne(collectionName, CollectionPreparer.identity(), new Document(idKey, id), new Document(), entityClass); } @@ -1122,8 +1138,7 @@ public class MongoTemplate } protected long doEstimatedCount(CollectionPreparer> collectionPreparer, - String collectionName, - EstimatedDocumentCountOptions options) { + String collectionName, EstimatedDocumentCountOptions options) { return execute(collectionName, collection -> collectionPreparer.prepare(collection).estimatedDocumentCount(options)); } @@ -2376,15 +2391,16 @@ public class MongoTemplate * The query document is specified as a standard {@link Document} and so is the fields specification. * * @param collectionName name of the collection to retrieve the objects from. + * @param collectionPreparer the preparer to prepare the collection for the actual use. * @param query the query document that specifies the criteria used to find a record. * @param fields the document that specifies the fields to be returned. * @param entityClass the parameterized type of the returned list. * @return the converted object or {@literal null} if none exists. */ @Nullable - protected T doFindOne(CollectionPreparer collectionPreparer, String collectionName, Document query, - Document fields, Class entityClass) { - return doFindOne(collectionPreparer, collectionName, query, fields, CursorPreparer.NO_OP_PREPARER, entityClass); + protected T doFindOne(String collectionName, CollectionPreparer> collectionPreparer, + Document query, Document fields, Class entityClass) { + return doFindOne(collectionName, collectionPreparer, query, fields, CursorPreparer.NO_OP_PREPARER, entityClass); } /** @@ -2392,17 +2408,18 @@ public class MongoTemplate * The query document is specified as a standard {@link Document} and so is the fields specification. * * @param collectionName name of the collection to retrieve the objects from. + * @param collectionPreparer the preparer to prepare the collection for the actual use. * @param query the query document that specifies the criteria used to find a record. * @param fields the document that specifies the fields to be returned. - * @param entityClass the parameterized type of the returned list. * @param preparer the preparer used to modify the cursor on execution. + * @param entityClass the parameterized type of the returned list. * @return the converted object or {@literal null} if none exists. * @since 2.2 */ @Nullable @SuppressWarnings("ConstantConditions") - protected T doFindOne(CollectionPreparer collectionPreparer, String collectionName, Document query, - Document fields, CursorPreparer preparer, Class entityClass) { + protected T doFindOne(String collectionName, CollectionPreparer> collectionPreparer, + Document query, Document fields, CursorPreparer preparer, Class entityClass) { MongoPersistentEntity entity = mappingContext.getPersistentEntity(entityClass); @@ -2424,14 +2441,15 @@ public class MongoTemplate * query document is specified as a standard Document and so is the fields specification. * * @param collectionName name of the collection to retrieve the objects from + * @param collectionPreparer the preparer to prepare the collection for the actual use. * @param query the query document that specifies the criteria used to find a record * @param fields the document that specifies the fields to be returned * @param entityClass the parameterized type of the returned list. * @return the List of converted objects. */ - protected List doFind(CollectionPreparer collectionPreparer, String collectionName, Document query, - Document fields, Class entityClass) { - return doFind(collectionPreparer, collectionName, query, fields, entityClass, null, + protected List doFind(String collectionName, CollectionPreparer> collectionPreparer, + Document query, Document fields, Class entityClass) { + return doFind(collectionName, collectionPreparer, query, fields, entityClass, null, new ReadDocumentCallback<>(this.mongoConverter, entityClass, collectionName)); } @@ -2441,6 +2459,7 @@ public class MongoTemplate * specified as a standard Document and so is the fields specification. * * @param collectionName name of the collection to retrieve the objects from. + * @param collectionPreparer the preparer to prepare the collection for the actual use. * @param query the query document that specifies the criteria used to find a record. * @param fields the document that specifies the fields to be returned. * @param entityClass the parameterized type of the returned list. @@ -2448,14 +2467,15 @@ public class MongoTemplate * (apply limits, skips and so on). * @return the {@link List} of converted objects. */ - protected List doFind(CollectionPreparer collectionPreparer, String collectionName, Document query, - Document fields, Class entityClass, CursorPreparer preparer) { - return doFind(collectionPreparer, collectionName, query, fields, entityClass, preparer, + protected List doFind(String collectionName, CollectionPreparer> collectionPreparer, + Document query, Document fields, Class entityClass, CursorPreparer preparer) { + return doFind(collectionName, collectionPreparer, query, fields, entityClass, preparer, new ReadDocumentCallback<>(mongoConverter, entityClass, collectionName)); } - protected List doFind(CollectionPreparer collectionPreparer, String collectionName, Document query, - Document fields, Class entityClass, @Nullable CursorPreparer preparer, DocumentCallback objectCallback) { + protected List doFind(String collectionName, + CollectionPreparer> collectionPreparer, Document query, Document fields, + Class entityClass, @Nullable CursorPreparer preparer, DocumentCallback objectCallback) { MongoPersistentEntity entity = mappingContext.getPersistentEntity(entityClass); @@ -2478,8 +2498,8 @@ public class MongoTemplate * * @since 2.0 */ - List doFind(CollectionPreparer collectionPreparer, String collectionName, Document query, Document fields, - Class sourceClass, Class targetClass, CursorPreparer preparer) { + List doFind(CollectionPreparer> collectionPreparer, String collectionName, + Document query, Document fields, Class sourceClass, Class targetClass, CursorPreparer preparer) { MongoPersistentEntity entity = mappingContext.getPersistentEntity(sourceClass); EntityProjection projection = operations.introspectProjection(targetClass, sourceClass); @@ -2900,8 +2920,7 @@ public class MongoTemplate private final @Nullable com.mongodb.client.model.Collation collation; public FindCallback(CollectionPreparer> collectionPreparer, Document query, - Document fields, - @Nullable com.mongodb.client.model.Collation collation) { + Document fields, @Nullable com.mongodb.client.model.Collation collation) { Assert.notNull(query, "Query must not be null"); Assert.notNull(fields, "Fields must not be null"); @@ -2970,8 +2989,7 @@ public class MongoTemplate private final Optional collation; FindAndRemoveCallback(CollectionPreparer> collectionPreparer, Document query, - Document fields, Document sort, - @Nullable Collation collation) { + Document fields, Document sort, @Nullable Collation collation) { this.collectionPreparer = collectionPreparer; this.query = query; @@ -3001,8 +3019,7 @@ public class MongoTemplate private final FindAndModifyOptions options; FindAndModifyCallback(CollectionPreparer> collectionPreparer, Document query, - Document fields, Document sort, - Object update, List arrayFilters, FindAndModifyOptions options) { + Document fields, Document sort, Object update, List arrayFilters, FindAndModifyOptions options) { this.collectionPreparer = collectionPreparer; this.query = query; @@ -3060,8 +3077,8 @@ public class MongoTemplate private final FindAndReplaceOptions options; FindAndReplaceCallback(CollectionPreparer> collectionPreparer, Document query, - Document fields, Document sort, - Document update, @Nullable com.mongodb.client.model.Collation collation, FindAndReplaceOptions options) { + Document fields, Document sort, Document update, @Nullable com.mongodb.client.model.Collation collation, + FindAndReplaceOptions options) { this.collectionPreparer = collectionPreparer; this.query = query; this.fields = fields; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveFindOperationSupport.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveFindOperationSupport.java index 8ff57f7f8..4dcf62aac 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveFindOperationSupport.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveFindOperationSupport.java @@ -170,7 +170,7 @@ class ReactiveFindOperationSupport implements ReactiveFindOperation { Document queryObject = query.getQueryObject(); Document fieldsObject = query.getFieldsObject(); - return template.doFind(ReactiveCollectionPreparerDelegate.of(query), getCollectionName(), queryObject, + return template.doFind(getCollectionName(), ReactiveCollectionPreparerDelegate.of(query), queryObject, fieldsObject, domainType, returnType, preparer != null ? preparer : getCursorPreparer(query)); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java index b71620984..1eb27c652 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java @@ -147,9 +147,18 @@ import com.mongodb.reactivestreams.client.MongoDatabase; * extract results. This class executes BSON queries or updates, initiating iteration over {@link FindPublisher} and * catching MongoDB exceptions and translating them to the generic, more informative exception hierarchy defined in the * org.springframework.dao package. Can be used within a service implementation via direct instantiation with a - * {@link SimpleReactiveMongoDatabaseFactory} reference, or get prepared in an application context and given to services - * as bean reference. Note: The {@link SimpleReactiveMongoDatabaseFactory} should always be configured as a bean in the - * application context, in the first case given to the service directly, in the second case to the prepared template. + * {@link ReactiveMongoDatabaseFactory} reference, or get prepared in an application context and given to services as + * bean reference. + *

+ * Note: The {@link ReactiveMongoDatabaseFactory} should always be configured as a bean in the application context, in + * the first case given to the service directly, in the second case to the prepared template. + *

{@link ReadPreference} and {@link com.mongodb.ReadConcern}

+ *

+ * {@code ReadPreference} and {@code ReadConcern} are generally considered from {@link Query} and + * {@link AggregationOptions} objects for the action to be executed on a particular {@link MongoCollection}. + *

+ * You can also set the default {@link #setReadPreference(ReadPreference) ReadPreference} on the template level to + * generally apply a {@link ReadPreference}. * * @author Mark Paluch * @author Christoph Strobl @@ -756,7 +765,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati public Mono findOne(Query query, Class entityClass, String collectionName) { if (ObjectUtils.isEmpty(query.getSortObject())) { - return doFindOne(ReactiveCollectionPreparerDelegate.of(query), collectionName, query.getQueryObject(), + return doFindOne(collectionName, ReactiveCollectionPreparerDelegate.of(query), query.getQueryObject(), query.getFieldsObject(), entityClass, new QueryFindPublisherPreparer(query, entityClass)); } @@ -812,7 +821,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati return findAll(entityClass, collectionName); } - return doFind(ReactiveCollectionPreparerDelegate.of(query), collectionName, query.getQueryObject(), + return doFind(collectionName, ReactiveCollectionPreparerDelegate.of(query), query.getQueryObject(), query.getFieldsObject(), entityClass, new QueryFindPublisherPreparer(query, entityClass)); } @@ -826,7 +835,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati String idKey = operations.getIdPropertyName(entityClass); - return doFindOne(CollectionPreparer.identity(), collectionName, new Document(idKey, id), null, entityClass, + return doFindOne(collectionName, CollectionPreparer.identity(), new Document(idKey, id), null, entityClass, (Collation) null); } @@ -1030,7 +1039,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati operations.forType(entityClass).getCollation(query).ifPresent(optionsToUse::collation); } - return doFindAndModify(ReactiveCollectionPreparerDelegate.of(query), collectionName, query.getQueryObject(), + return doFindAndModify(collectionName, ReactiveCollectionPreparerDelegate.of(query), query.getQueryObject(), query.getFieldsObject(), getMappedSortObject(query, entityClass), entityClass, update, optionsToUse); } @@ -1073,7 +1082,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati mapped.getCollection())); }).flatMap(it -> { - Mono afterFindAndReplace = doFindAndReplace(collectionPreparer, it.getCollection(), mappedQuery, + Mono afterFindAndReplace = doFindAndReplace(it.getCollection(), collectionPreparer, mappedQuery, mappedFields, mappedSort, queryContext.getCollation(entityType).orElse(null), entityType, it.getTarget(), options, projection); return afterFindAndReplace.flatMap(saved -> { @@ -1093,7 +1102,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati public Mono findAndRemove(Query query, Class entityClass, String collectionName) { operations.forType(entityClass).getCollation(query); - return doFindAndRemove(ReactiveCollectionPreparerDelegate.of(query), collectionName, query.getQueryObject(), + return doFindAndRemove(collectionName, ReactiveCollectionPreparerDelegate.of(query), query.getQueryObject(), query.getFieldsObject(), getMappedSortObject(query, entityClass), operations.forType(entityClass).getCollation(query).orElse(null), entityClass); } @@ -1886,7 +1895,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati collectionName); } - return doFind(collectionPreparer, collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass, + return doFind(collectionName, collectionPreparer, query.getQueryObject(), query.getFieldsObject(), entityClass, new TailingQueryFindPublisherPreparer(query, entityClass)); } @@ -2144,17 +2153,18 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati * The query document is specified as a standard {@link Document} and so is the fields specification. * * @param collectionName name of the collection to retrieve the objects from. + * @param collectionPreparer the preparer to prepare the collection for the actual use. * @param query the query document that specifies the criteria used to find a record. * @param fields the document that specifies the fields to be returned. * @param entityClass the parameterized type of the returned list. * @param collation can be {@literal null}. * @return the {@link List} of converted objects. */ - protected Mono doFindOne(CollectionPreparer> collectionPreparer, - String collectionName, Document query, @Nullable Document fields, Class entityClass, - @Nullable Collation collation) { + protected Mono doFindOne(String collectionName, + CollectionPreparer> collectionPreparer, Document query, @Nullable Document fields, + Class entityClass, @Nullable Collation collation) { - return doFindOne(collectionPreparer, collectionName, query, fields, entityClass, + return doFindOne(collectionName, collectionPreparer, query, fields, entityClass, findPublisher -> collation != null ? findPublisher.collation(collation.toMongoCollation()) : findPublisher); } @@ -2163,6 +2173,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati * The query document is specified as a standard {@link Document} and so is the fields specification. * * @param collectionName name of the collection to retrieve the objects from. + * @param collectionPreparer the preparer to prepare the collection for the actual use. * @param query the query document that specifies the criteria used to find a record. * @param fields the document that specifies the fields to be returned. * @param entityClass the parameterized type of the returned list. @@ -2170,9 +2181,9 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati * @return the {@link List} of converted objects. * @since 2.2 */ - protected Mono doFindOne(CollectionPreparer> collectionPreparer, - String collectionName, Document query, @Nullable Document fields, Class entityClass, - FindPublisherPreparer preparer) { + protected Mono doFindOne(String collectionName, + CollectionPreparer> collectionPreparer, Document query, @Nullable Document fields, + Class entityClass, FindPublisherPreparer preparer) { MongoPersistentEntity entity = mappingContext.getPersistentEntity(entityClass); @@ -2195,14 +2206,15 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati * query document is specified as a standard Document and so is the fields specification. * * @param collectionName name of the collection to retrieve the objects from + * @param collectionPreparer the preparer to prepare the collection for the actual use. * @param query the query document that specifies the criteria used to find a record * @param fields the document that specifies the fields to be returned * @param entityClass the parameterized type of the returned list. * @return the List of converted objects. */ - protected Flux doFind(CollectionPreparer> collectionPreparer, String collectionName, + protected Flux doFind(String collectionName, CollectionPreparer> collectionPreparer, Document query, Document fields, Class entityClass) { - return doFind(collectionPreparer, collectionName, query, fields, entityClass, null, + return doFind(collectionName, collectionPreparer, query, fields, entityClass, null, new ReadDocumentCallback<>(this.mongoConverter, entityClass, collectionName)); } @@ -2212,6 +2224,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati * specified as a standard Document and so is the fields specification. * * @param collectionName name of the collection to retrieve the objects from. + * @param collectionPreparer the preparer to prepare the collection for the actual use. * @param query the query document that specifies the criteria used to find a record. * @param fields the document that specifies the fields to be returned. * @param entityClass the parameterized type of the returned list. @@ -2219,15 +2232,15 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati * the result set, (apply limits, skips and so on). * @return the {@link List} of converted objects. */ - protected Flux doFind(CollectionPreparer> collectionPreparer, String collectionName, + protected Flux doFind(String collectionName, CollectionPreparer> collectionPreparer, Document query, Document fields, Class entityClass, FindPublisherPreparer preparer) { - return doFind(collectionPreparer, collectionName, query, fields, entityClass, preparer, + return doFind(collectionName, collectionPreparer, query, fields, entityClass, preparer, new ReadDocumentCallback<>(mongoConverter, entityClass, collectionName)); } - protected Flux doFind(CollectionPreparer> collectionPreparer, - String collectionName, Document query, Document fields, Class entityClass, - @Nullable FindPublisherPreparer preparer, DocumentCallback objectCallback) { + protected Flux doFind(String collectionName, + CollectionPreparer> collectionPreparer, Document query, Document fields, + Class entityClass, @Nullable FindPublisherPreparer preparer, DocumentCallback objectCallback) { MongoPersistentEntity entity = mappingContext.getPersistentEntity(entityClass); @@ -2250,7 +2263,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati * * @since 2.0 */ - Flux doFind(CollectionPreparer> collectionPreparer, String collectionName, + Flux doFind(String collectionName, CollectionPreparer> collectionPreparer, Document query, Document fields, Class sourceClass, Class targetClass, FindPublisherPreparer preparer) { MongoPersistentEntity entity = mappingContext.getPersistentEntity(sourceClass); @@ -2283,15 +2296,16 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati * The first document that matches the query is returned and also removed from the collection in the database.
* The query document is specified as a standard Document and so is the fields specification. * - * @param collectionName name of the collection to retrieve the objects from - * @param query the query document that specifies the criteria used to find a record - * @param collation collation + * @param collectionName name of the collection to retrieve the objects from. + * @param collectionPreparer the preparer to prepare the collection for the actual use. + * @param query the query document that specifies the criteria used to find a record. + * @param collation collation. * @param entityClass the parameterized type of the returned list. * @return the List of converted objects. */ - protected Mono doFindAndRemove(CollectionPreparer> collectionPreparer, - String collectionName, Document query, Document fields, Document sort, @Nullable Collation collation, - Class entityClass) { + protected Mono doFindAndRemove(String collectionName, + CollectionPreparer> collectionPreparer, Document query, Document fields, Document sort, + @Nullable Collation collation, Class entityClass) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(String.format("findAndRemove using query: %s fields: %s sort: %s for class: %s in collection: %s", @@ -2305,9 +2319,9 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati new ReadDocumentCallback<>(this.mongoConverter, entityClass, collectionName), collectionName); } - protected Mono doFindAndModify(CollectionPreparer> collectionPreparer, - String collectionName, Document query, Document fields, Document sort, Class entityClass, - UpdateDefinition update, FindAndModifyOptions options) { + protected Mono doFindAndModify(String collectionName, + CollectionPreparer> collectionPreparer, Document query, Document fields, Document sort, + Class entityClass, UpdateDefinition update, FindAndModifyOptions options) { MongoPersistentEntity entity = mappingContext.getPersistentEntity(entityClass); UpdateContext updateContext = queryOperations.updateSingleContext(update, query, false); @@ -2337,6 +2351,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati * Customize this part for findAndReplace. * * @param collectionName The name of the collection to perform the operation in. + * @param collectionPreparer the preparer to prepare the collection for the actual use. * @param mappedQuery the query to look up documents. * @param mappedFields the fields to project the result to. * @param mappedSort the sort to be applied when executing the query. @@ -2349,14 +2364,14 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati * {@literal false} and {@link FindAndReplaceOptions#isUpsert() upsert} is {@literal false}. * @since 2.1 */ - protected Mono doFindAndReplace(CollectionPreparer> collectionPreparer, - String collectionName, Document mappedQuery, Document mappedFields, Document mappedSort, - com.mongodb.client.model.Collation collation, Class entityType, Document replacement, + protected Mono doFindAndReplace(String collectionName, + CollectionPreparer> collectionPreparer, Document mappedQuery, Document mappedFields, + Document mappedSort, com.mongodb.client.model.Collation collation, Class entityType, Document replacement, FindAndReplaceOptions options, Class resultType) { EntityProjection projection = operations.introspectProjection(resultType, entityType); - return doFindAndReplace(collectionPreparer, collectionName, mappedQuery, mappedFields, mappedSort, collation, + return doFindAndReplace(collectionName, collectionPreparer, mappedQuery, mappedFields, mappedSort, collation, entityType, replacement, options, projection); } @@ -2364,6 +2379,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati * Customize this part for findAndReplace. * * @param collectionName The name of the collection to perform the operation in. + * @param collectionPreparer the preparer to prepare the collection for the actual use. * @param mappedQuery the query to look up documents. * @param mappedFields the fields to project the result to. * @param mappedSort the sort to be applied when executing the query. @@ -2376,9 +2392,9 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati * {@literal false} and {@link FindAndReplaceOptions#isUpsert() upsert} is {@literal false}. * @since 3.4 */ - private Mono doFindAndReplace(CollectionPreparer> collectionPreparer, - String collectionName, Document mappedQuery, Document mappedFields, Document mappedSort, - com.mongodb.client.model.Collation collation, Class entityType, Document replacement, + private Mono doFindAndReplace(String collectionName, + CollectionPreparer> collectionPreparer, Document mappedQuery, Document mappedFields, + Document mappedSort, com.mongodb.client.model.Collation collation, Class entityType, Document replacement, FindAndReplaceOptions options, EntityProjection projection) { return Mono.defer(() -> { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReadConcernAware.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReadConcernAware.java index fc3e47e09..a916220d0 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReadConcernAware.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReadConcernAware.java @@ -26,6 +26,8 @@ import com.mongodb.ReadConcern; * * @author Mark Paluch * @since 4.1 + * @see org.springframework.data.mongodb.core.query.Query + * @see org.springframework.data.mongodb.core.aggregation.AggregationOptions */ public interface ReadConcernAware { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReadPreferenceAware.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReadPreferenceAware.java index 334a9d5c5..09c87af05 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReadPreferenceAware.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReadPreferenceAware.java @@ -27,6 +27,8 @@ import com.mongodb.ReadPreference; * @author Christoph Strobl * @author Mark Paluch * @since 2.2 + * @see org.springframework.data.mongodb.core.query.Query + * @see org.springframework.data.mongodb.core.aggregation.AggregationOptions */ public interface ReadPreferenceAware { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java index 07d4ccb17..962685890 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java @@ -387,7 +387,7 @@ public class ReactiveMongoTemplateUnitTests { @Test // DATAMONGO-1719 void appliesFieldsWhenInterfaceProjectionIsClosedAndQueryDoesNotDefineFields() { - template.doFind(CollectionPreparer.identity(), "star-wars", new Document(), new Document(), Person.class, + template.doFind("star-wars", CollectionPreparer.identity(), new Document(), new Document(), Person.class, PersonProjection.class, FindPublisherPreparer.NO_OP_PREPARER).subscribe(); verify(findPublisher).projection(eq(new Document("firstname", 1))); @@ -396,7 +396,7 @@ public class ReactiveMongoTemplateUnitTests { @Test // DATAMONGO-1719 void doesNotApplyFieldsWhenInterfaceProjectionIsClosedAndQueryDefinesFields() { - template.doFind(CollectionPreparer.identity(), "star-wars", new Document(), new Document("bar", 1), Person.class, + template.doFind("star-wars", CollectionPreparer.identity(), new Document(), new Document("bar", 1), Person.class, PersonProjection.class, FindPublisherPreparer.NO_OP_PREPARER).subscribe(); verify(findPublisher).projection(eq(new Document("bar", 1))); @@ -405,7 +405,7 @@ public class ReactiveMongoTemplateUnitTests { @Test // DATAMONGO-1719 void doesNotApplyFieldsWhenInterfaceProjectionIsOpen() { - template.doFind(CollectionPreparer.identity(), "star-wars", new Document(), new Document(), Person.class, + template.doFind("star-wars", CollectionPreparer.identity(), new Document(), new Document(), Person.class, PersonSpELProjection.class, FindPublisherPreparer.NO_OP_PREPARER).subscribe(); verify(findPublisher, never()).projection(any()); @@ -414,7 +414,7 @@ public class ReactiveMongoTemplateUnitTests { @Test // DATAMONGO-1719, DATAMONGO-2041 void appliesFieldsToDtoProjection() { - template.doFind(CollectionPreparer.identity(), "star-wars", new Document(), new Document(), Person.class, + template.doFind("star-wars", CollectionPreparer.identity(), new Document(), new Document(), Person.class, Jedi.class, FindPublisherPreparer.NO_OP_PREPARER).subscribe(); verify(findPublisher).projection(eq(new Document("firstname", 1))); @@ -423,7 +423,7 @@ public class ReactiveMongoTemplateUnitTests { @Test // DATAMONGO-1719 void doesNotApplyFieldsToDtoProjectionWhenQueryDefinesFields() { - template.doFind(CollectionPreparer.identity(), "star-wars", new Document(), new Document("bar", 1), Person.class, + template.doFind("star-wars", CollectionPreparer.identity(), new Document(), new Document("bar", 1), Person.class, Jedi.class, FindPublisherPreparer.NO_OP_PREPARER).subscribe(); verify(findPublisher).projection(eq(new Document("bar", 1))); @@ -432,7 +432,7 @@ public class ReactiveMongoTemplateUnitTests { @Test // DATAMONGO-1719 void doesNotApplyFieldsWhenTargetIsNotAProjection() { - template.doFind(CollectionPreparer.identity(), "star-wars", new Document(), new Document(), Person.class, + template.doFind("star-wars", CollectionPreparer.identity(), new Document(), new Document(), Person.class, Person.class, FindPublisherPreparer.NO_OP_PREPARER).subscribe(); verify(findPublisher, never()).projection(any()); @@ -441,7 +441,7 @@ public class ReactiveMongoTemplateUnitTests { @Test // DATAMONGO-1719 void doesNotApplyFieldsWhenTargetExtendsDomainType() { - template.doFind(CollectionPreparer.identity(), "star-wars", new Document(), new Document(), Person.class, + template.doFind("star-wars", CollectionPreparer.identity(), new Document(), new Document(), Person.class, PersonExtended.class, FindPublisherPreparer.NO_OP_PREPARER).subscribe(); verify(findPublisher, never()).projection(any());