Browse Source

Polishing.

Documentation, refine parameter ordering.

Original Pull Request: #4288
pull/4310/head
Mark Paluch 3 years ago committed by Christoph Strobl
parent
commit
e56f6ce87f
No known key found for this signature in database
GPG Key ID: 8CC1AB53391458C8
  1. 77
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
  2. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveFindOperationSupport.java
  3. 98
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java
  4. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReadConcernAware.java
  5. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReadPreferenceAware.java
  6. 14
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java

77
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; 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.
* <p>
* 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.
* <h3>{@link ReadPreference} and {@link com.mongodb.ReadConcern}</h3>
* <p>
* {@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}.
* <p>
* You can also set the default {@link #setReadPreference(ReadPreference) ReadPreference} on the template level to
* generally apply a {@link ReadPreference}.
* *
* @author Thomas Risberg * @author Thomas Risberg
* @author Graeme Rocher * @author Graeme Rocher
@ -778,7 +794,7 @@ public class MongoTemplate
if (ObjectUtils.isEmpty(query.getSortObject())) { 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); new QueryCursorPreparer(query, entityClass), entityClass);
} else { } else {
query.limit(1); query.limit(1);
@ -827,7 +843,7 @@ public class MongoTemplate
Assert.notNull(collectionName, "CollectionName must not be null"); Assert.notNull(collectionName, "CollectionName must not be null");
Assert.notNull(entityClass, "EntityClass 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)); new QueryCursorPreparer(query, entityClass));
} }
@ -847,7 +863,7 @@ public class MongoTemplate
String idKey = operations.getIdPropertyName(entityClass); 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); entityClass);
} }
@ -1122,8 +1138,7 @@ public class MongoTemplate
} }
protected long doEstimatedCount(CollectionPreparer<MongoCollection<Document>> collectionPreparer, protected long doEstimatedCount(CollectionPreparer<MongoCollection<Document>> collectionPreparer,
String collectionName, String collectionName, EstimatedDocumentCountOptions options) {
EstimatedDocumentCountOptions options) {
return execute(collectionName, return execute(collectionName,
collection -> collectionPreparer.prepare(collection).estimatedDocumentCount(options)); 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. * 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 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 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 fields the document that specifies the fields to be returned.
* @param entityClass the parameterized type of the returned list. * @param entityClass the parameterized type of the returned list.
* @return the converted object or {@literal null} if none exists. * @return the converted object or {@literal null} if none exists.
*/ */
@Nullable @Nullable
protected <T> T doFindOne(CollectionPreparer collectionPreparer, String collectionName, Document query, protected <T> T doFindOne(String collectionName, CollectionPreparer<MongoCollection<Document>> collectionPreparer,
Document fields, Class<T> entityClass) { Document query, Document fields, Class<T> entityClass) {
return doFindOne(collectionPreparer, collectionName, query, fields, CursorPreparer.NO_OP_PREPARER, 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. * 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 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 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 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 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. * @return the converted object or {@literal null} if none exists.
* @since 2.2 * @since 2.2
*/ */
@Nullable @Nullable
@SuppressWarnings("ConstantConditions") @SuppressWarnings("ConstantConditions")
protected <T> T doFindOne(CollectionPreparer collectionPreparer, String collectionName, Document query, protected <T> T doFindOne(String collectionName, CollectionPreparer<MongoCollection<Document>> collectionPreparer,
Document fields, CursorPreparer preparer, Class<T> entityClass) { Document query, Document fields, CursorPreparer preparer, Class<T> entityClass) {
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(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. * 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 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 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 fields the document that specifies the fields to be returned
* @param entityClass the parameterized type of the returned list. * @param entityClass the parameterized type of the returned list.
* @return the List of converted objects. * @return the List of converted objects.
*/ */
protected <T> List<T> doFind(CollectionPreparer collectionPreparer, String collectionName, Document query, protected <T> List<T> doFind(String collectionName, CollectionPreparer<MongoCollection<Document>> collectionPreparer,
Document fields, Class<T> entityClass) { Document query, Document fields, Class<T> entityClass) {
return doFind(collectionPreparer, collectionName, query, fields, entityClass, null, return doFind(collectionName, collectionPreparer, query, fields, entityClass, null,
new ReadDocumentCallback<>(this.mongoConverter, entityClass, collectionName)); new ReadDocumentCallback<>(this.mongoConverter, entityClass, collectionName));
} }
@ -2441,6 +2459,7 @@ public class MongoTemplate
* specified as a standard Document and so is the fields specification. * specified as a standard Document and so is the fields specification.
* *
* @param collectionName name of the collection to retrieve the objects from. * @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 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 fields the document that specifies the fields to be returned.
* @param entityClass the parameterized type of the returned list. * @param entityClass the parameterized type of the returned list.
@ -2448,14 +2467,15 @@ public class MongoTemplate
* (apply limits, skips and so on). * (apply limits, skips and so on).
* @return the {@link List} of converted objects. * @return the {@link List} of converted objects.
*/ */
protected <T> List<T> doFind(CollectionPreparer collectionPreparer, String collectionName, Document query, protected <T> List<T> doFind(String collectionName, CollectionPreparer<MongoCollection<Document>> collectionPreparer,
Document fields, Class<T> entityClass, CursorPreparer preparer) { Document query, Document fields, Class<T> entityClass, CursorPreparer preparer) {
return doFind(collectionPreparer, collectionName, query, fields, entityClass, preparer, return doFind(collectionName, collectionPreparer, query, fields, entityClass, preparer,
new ReadDocumentCallback<>(mongoConverter, entityClass, collectionName)); new ReadDocumentCallback<>(mongoConverter, entityClass, collectionName));
} }
protected <S, T> List<T> doFind(CollectionPreparer collectionPreparer, String collectionName, Document query, protected <S, T> List<T> doFind(String collectionName,
Document fields, Class<S> entityClass, @Nullable CursorPreparer preparer, DocumentCallback<T> objectCallback) { CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query, Document fields,
Class<S> entityClass, @Nullable CursorPreparer preparer, DocumentCallback<T> objectCallback) {
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass); MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
@ -2478,8 +2498,8 @@ public class MongoTemplate
* *
* @since 2.0 * @since 2.0
*/ */
<S, T> List<T> doFind(CollectionPreparer collectionPreparer, String collectionName, Document query, Document fields, <S, T> List<T> doFind(CollectionPreparer<MongoCollection<Document>> collectionPreparer, String collectionName,
Class<S> sourceClass, Class<T> targetClass, CursorPreparer preparer) { Document query, Document fields, Class<S> sourceClass, Class<T> targetClass, CursorPreparer preparer) {
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(sourceClass); MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(sourceClass);
EntityProjection<T, S> projection = operations.introspectProjection(targetClass, sourceClass); EntityProjection<T, S> projection = operations.introspectProjection(targetClass, sourceClass);
@ -2900,8 +2920,7 @@ public class MongoTemplate
private final @Nullable com.mongodb.client.model.Collation collation; private final @Nullable com.mongodb.client.model.Collation collation;
public FindCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query, public FindCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query,
Document fields, Document fields, @Nullable com.mongodb.client.model.Collation collation) {
@Nullable com.mongodb.client.model.Collation collation) {
Assert.notNull(query, "Query must not be null"); Assert.notNull(query, "Query must not be null");
Assert.notNull(fields, "Fields must not be null"); Assert.notNull(fields, "Fields must not be null");
@ -2970,8 +2989,7 @@ public class MongoTemplate
private final Optional<Collation> collation; private final Optional<Collation> collation;
FindAndRemoveCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query, FindAndRemoveCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query,
Document fields, Document sort, Document fields, Document sort, @Nullable Collation collation) {
@Nullable Collation collation) {
this.collectionPreparer = collectionPreparer; this.collectionPreparer = collectionPreparer;
this.query = query; this.query = query;
@ -3001,8 +3019,7 @@ public class MongoTemplate
private final FindAndModifyOptions options; private final FindAndModifyOptions options;
FindAndModifyCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query, FindAndModifyCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query,
Document fields, Document sort, Document fields, Document sort, Object update, List<Document> arrayFilters, FindAndModifyOptions options) {
Object update, List<Document> arrayFilters, FindAndModifyOptions options) {
this.collectionPreparer = collectionPreparer; this.collectionPreparer = collectionPreparer;
this.query = query; this.query = query;
@ -3060,8 +3077,8 @@ public class MongoTemplate
private final FindAndReplaceOptions options; private final FindAndReplaceOptions options;
FindAndReplaceCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query, FindAndReplaceCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query,
Document fields, Document sort, Document fields, Document sort, Document update, @Nullable com.mongodb.client.model.Collation collation,
Document update, @Nullable com.mongodb.client.model.Collation collation, FindAndReplaceOptions options) { FindAndReplaceOptions options) {
this.collectionPreparer = collectionPreparer; this.collectionPreparer = collectionPreparer;
this.query = query; this.query = query;
this.fields = fields; this.fields = fields;

2
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 queryObject = query.getQueryObject();
Document fieldsObject = query.getFieldsObject(); 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)); fieldsObject, domainType, returnType, preparer != null ? preparer : getCursorPreparer(query));
} }

98
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 * 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 * 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 * 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 * {@link ReactiveMongoDatabaseFactory} reference, or get prepared in an application context and given to services as
* as bean reference. Note: The {@link SimpleReactiveMongoDatabaseFactory} should always be configured as a bean in the * bean reference.
* application context, in the first case given to the service directly, in the second case to the prepared template. * <p>
* 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.
* <h3>{@link ReadPreference} and {@link com.mongodb.ReadConcern}</h3>
* <p>
* {@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}.
* <p>
* You can also set the default {@link #setReadPreference(ReadPreference) ReadPreference} on the template level to
* generally apply a {@link ReadPreference}.
* *
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl * @author Christoph Strobl
@ -756,7 +765,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
public <T> Mono<T> findOne(Query query, Class<T> entityClass, String collectionName) { public <T> Mono<T> findOne(Query query, Class<T> entityClass, String collectionName) {
if (ObjectUtils.isEmpty(query.getSortObject())) { 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)); query.getFieldsObject(), entityClass, new QueryFindPublisherPreparer(query, entityClass));
} }
@ -812,7 +821,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
return findAll(entityClass, collectionName); 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)); query.getFieldsObject(), entityClass, new QueryFindPublisherPreparer(query, entityClass));
} }
@ -826,7 +835,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
String idKey = operations.getIdPropertyName(entityClass); 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); (Collation) null);
} }
@ -1030,7 +1039,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
operations.forType(entityClass).getCollation(query).ifPresent(optionsToUse::collation); 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); query.getFieldsObject(), getMappedSortObject(query, entityClass), entityClass, update, optionsToUse);
} }
@ -1073,7 +1082,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
mapped.getCollection())); mapped.getCollection()));
}).flatMap(it -> { }).flatMap(it -> {
Mono<T> afterFindAndReplace = doFindAndReplace(collectionPreparer, it.getCollection(), mappedQuery, Mono<T> afterFindAndReplace = doFindAndReplace(it.getCollection(), collectionPreparer, mappedQuery,
mappedFields, mappedSort, queryContext.getCollation(entityType).orElse(null), entityType, it.getTarget(), mappedFields, mappedSort, queryContext.getCollation(entityType).orElse(null), entityType, it.getTarget(),
options, projection); options, projection);
return afterFindAndReplace.flatMap(saved -> { return afterFindAndReplace.flatMap(saved -> {
@ -1093,7 +1102,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
public <T> Mono<T> findAndRemove(Query query, Class<T> entityClass, String collectionName) { public <T> Mono<T> findAndRemove(Query query, Class<T> entityClass, String collectionName) {
operations.forType(entityClass).getCollation(query); 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), query.getFieldsObject(), getMappedSortObject(query, entityClass),
operations.forType(entityClass).getCollation(query).orElse(null), entityClass); operations.forType(entityClass).getCollation(query).orElse(null), entityClass);
} }
@ -1886,7 +1895,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
collectionName); collectionName);
} }
return doFind(collectionPreparer, collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass, return doFind(collectionName, collectionPreparer, query.getQueryObject(), query.getFieldsObject(), entityClass,
new TailingQueryFindPublisherPreparer(query, 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. * 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 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 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 fields the document that specifies the fields to be returned.
* @param entityClass the parameterized type of the returned list. * @param entityClass the parameterized type of the returned list.
* @param collation can be {@literal null}. * @param collation can be {@literal null}.
* @return the {@link List} of converted objects. * @return the {@link List} of converted objects.
*/ */
protected <T> Mono<T> doFindOne(CollectionPreparer<MongoCollection<Document>> collectionPreparer, protected <T> Mono<T> doFindOne(String collectionName,
String collectionName, Document query, @Nullable Document fields, Class<T> entityClass, CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query, @Nullable Document fields,
@Nullable Collation collation) { Class<T> 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); 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. * 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 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 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 fields the document that specifies the fields to be returned.
* @param entityClass the parameterized type of the returned list. * @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. * @return the {@link List} of converted objects.
* @since 2.2 * @since 2.2
*/ */
protected <T> Mono<T> doFindOne(CollectionPreparer<MongoCollection<Document>> collectionPreparer, protected <T> Mono<T> doFindOne(String collectionName,
String collectionName, Document query, @Nullable Document fields, Class<T> entityClass, CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query, @Nullable Document fields,
FindPublisherPreparer preparer) { Class<T> entityClass, FindPublisherPreparer preparer) {
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass); 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. * 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 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 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 fields the document that specifies the fields to be returned
* @param entityClass the parameterized type of the returned list. * @param entityClass the parameterized type of the returned list.
* @return the List of converted objects. * @return the List of converted objects.
*/ */
protected <T> Flux<T> doFind(CollectionPreparer<MongoCollection<Document>> collectionPreparer, String collectionName, protected <T> Flux<T> doFind(String collectionName, CollectionPreparer<MongoCollection<Document>> collectionPreparer,
Document query, Document fields, Class<T> entityClass) { Document query, Document fields, Class<T> entityClass) {
return doFind(collectionPreparer, collectionName, query, fields, entityClass, null, return doFind(collectionName, collectionPreparer, query, fields, entityClass, null,
new ReadDocumentCallback<>(this.mongoConverter, entityClass, collectionName)); 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. * specified as a standard Document and so is the fields specification.
* *
* @param collectionName name of the collection to retrieve the objects from. * @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 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 fields the document that specifies the fields to be returned.
* @param entityClass the parameterized type of the returned list. * @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). * the result set, (apply limits, skips and so on).
* @return the {@link List} of converted objects. * @return the {@link List} of converted objects.
*/ */
protected <T> Flux<T> doFind(CollectionPreparer<MongoCollection<Document>> collectionPreparer, String collectionName, protected <T> Flux<T> doFind(String collectionName, CollectionPreparer<MongoCollection<Document>> collectionPreparer,
Document query, Document fields, Class<T> entityClass, FindPublisherPreparer preparer) { Document query, Document fields, Class<T> entityClass, FindPublisherPreparer preparer) {
return doFind(collectionPreparer, collectionName, query, fields, entityClass, preparer, return doFind(collectionName, collectionPreparer, query, fields, entityClass, preparer,
new ReadDocumentCallback<>(mongoConverter, entityClass, collectionName)); new ReadDocumentCallback<>(mongoConverter, entityClass, collectionName));
} }
protected <S, T> Flux<T> doFind(CollectionPreparer<MongoCollection<Document>> collectionPreparer, protected <S, T> Flux<T> doFind(String collectionName,
String collectionName, Document query, Document fields, Class<S> entityClass, CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query, Document fields,
@Nullable FindPublisherPreparer preparer, DocumentCallback<T> objectCallback) { Class<S> entityClass, @Nullable FindPublisherPreparer preparer, DocumentCallback<T> objectCallback) {
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass); MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
@ -2250,7 +2263,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* *
* @since 2.0 * @since 2.0
*/ */
<S, T> Flux<T> doFind(CollectionPreparer<MongoCollection<Document>> collectionPreparer, String collectionName, <S, T> Flux<T> doFind(String collectionName, CollectionPreparer<MongoCollection<Document>> collectionPreparer,
Document query, Document fields, Class<S> sourceClass, Class<T> targetClass, FindPublisherPreparer preparer) { Document query, Document fields, Class<S> sourceClass, Class<T> targetClass, FindPublisherPreparer preparer) {
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(sourceClass); 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. <br /> * The first document that matches the query is returned and also removed from the collection in the database. <br />
* The query document is specified as a standard Document and so is the fields specification. * 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 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 collectionPreparer the preparer to prepare the collection for the actual use.
* @param collation collation * @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. * @param entityClass the parameterized type of the returned list.
* @return the List of converted objects. * @return the List of converted objects.
*/ */
protected <T> Mono<T> doFindAndRemove(CollectionPreparer<MongoCollection<Document>> collectionPreparer, protected <T> Mono<T> doFindAndRemove(String collectionName,
String collectionName, Document query, Document fields, Document sort, @Nullable Collation collation, CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query, Document fields, Document sort,
Class<T> entityClass) { @Nullable Collation collation, Class<T> entityClass) {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("findAndRemove using query: %s fields: %s sort: %s for class: %s in collection: %s", 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); new ReadDocumentCallback<>(this.mongoConverter, entityClass, collectionName), collectionName);
} }
protected <T> Mono<T> doFindAndModify(CollectionPreparer<MongoCollection<Document>> collectionPreparer, protected <T> Mono<T> doFindAndModify(String collectionName,
String collectionName, Document query, Document fields, Document sort, Class<T> entityClass, CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query, Document fields, Document sort,
UpdateDefinition update, FindAndModifyOptions options) { Class<T> entityClass, UpdateDefinition update, FindAndModifyOptions options) {
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass); MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
UpdateContext updateContext = queryOperations.updateSingleContext(update, query, false); UpdateContext updateContext = queryOperations.updateSingleContext(update, query, false);
@ -2337,6 +2351,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* Customize this part for findAndReplace. * Customize this part for findAndReplace.
* *
* @param collectionName The name of the collection to perform the operation in. * @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 mappedQuery the query to look up documents.
* @param mappedFields the fields to project the result to. * @param mappedFields the fields to project the result to.
* @param mappedSort the sort to be applied when executing the query. * @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}. * {@literal false} and {@link FindAndReplaceOptions#isUpsert() upsert} is {@literal false}.
* @since 2.1 * @since 2.1
*/ */
protected <T> Mono<T> doFindAndReplace(CollectionPreparer<MongoCollection<Document>> collectionPreparer, protected <T> Mono<T> doFindAndReplace(String collectionName,
String collectionName, Document mappedQuery, Document mappedFields, Document mappedSort, CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document mappedQuery, Document mappedFields,
com.mongodb.client.model.Collation collation, Class<?> entityType, Document replacement, Document mappedSort, com.mongodb.client.model.Collation collation, Class<?> entityType, Document replacement,
FindAndReplaceOptions options, Class<T> resultType) { FindAndReplaceOptions options, Class<T> resultType) {
EntityProjection<T, ?> projection = operations.introspectProjection(resultType, entityType); EntityProjection<T, ?> 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); entityType, replacement, options, projection);
} }
@ -2364,6 +2379,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
* Customize this part for findAndReplace. * Customize this part for findAndReplace.
* *
* @param collectionName The name of the collection to perform the operation in. * @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 mappedQuery the query to look up documents.
* @param mappedFields the fields to project the result to. * @param mappedFields the fields to project the result to.
* @param mappedSort the sort to be applied when executing the query. * @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}. * {@literal false} and {@link FindAndReplaceOptions#isUpsert() upsert} is {@literal false}.
* @since 3.4 * @since 3.4
*/ */
private <T> Mono<T> doFindAndReplace(CollectionPreparer<MongoCollection<Document>> collectionPreparer, private <T> Mono<T> doFindAndReplace(String collectionName,
String collectionName, Document mappedQuery, Document mappedFields, Document mappedSort, CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document mappedQuery, Document mappedFields,
com.mongodb.client.model.Collation collation, Class<?> entityType, Document replacement, Document mappedSort, com.mongodb.client.model.Collation collation, Class<?> entityType, Document replacement,
FindAndReplaceOptions options, EntityProjection<T, ?> projection) { FindAndReplaceOptions options, EntityProjection<T, ?> projection) {
return Mono.defer(() -> { return Mono.defer(() -> {

2
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReadConcernAware.java

@ -26,6 +26,8 @@ import com.mongodb.ReadConcern;
* *
* @author Mark Paluch * @author Mark Paluch
* @since 4.1 * @since 4.1
* @see org.springframework.data.mongodb.core.query.Query
* @see org.springframework.data.mongodb.core.aggregation.AggregationOptions
*/ */
public interface ReadConcernAware { public interface ReadConcernAware {

2
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 Christoph Strobl
* @author Mark Paluch * @author Mark Paluch
* @since 2.2 * @since 2.2
* @see org.springframework.data.mongodb.core.query.Query
* @see org.springframework.data.mongodb.core.aggregation.AggregationOptions
*/ */
public interface ReadPreferenceAware { public interface ReadPreferenceAware {

14
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java

@ -387,7 +387,7 @@ public class ReactiveMongoTemplateUnitTests {
@Test // DATAMONGO-1719 @Test // DATAMONGO-1719
void appliesFieldsWhenInterfaceProjectionIsClosedAndQueryDoesNotDefineFields() { 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(); PersonProjection.class, FindPublisherPreparer.NO_OP_PREPARER).subscribe();
verify(findPublisher).projection(eq(new Document("firstname", 1))); verify(findPublisher).projection(eq(new Document("firstname", 1)));
@ -396,7 +396,7 @@ public class ReactiveMongoTemplateUnitTests {
@Test // DATAMONGO-1719 @Test // DATAMONGO-1719
void doesNotApplyFieldsWhenInterfaceProjectionIsClosedAndQueryDefinesFields() { 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(); PersonProjection.class, FindPublisherPreparer.NO_OP_PREPARER).subscribe();
verify(findPublisher).projection(eq(new Document("bar", 1))); verify(findPublisher).projection(eq(new Document("bar", 1)));
@ -405,7 +405,7 @@ public class ReactiveMongoTemplateUnitTests {
@Test // DATAMONGO-1719 @Test // DATAMONGO-1719
void doesNotApplyFieldsWhenInterfaceProjectionIsOpen() { 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(); PersonSpELProjection.class, FindPublisherPreparer.NO_OP_PREPARER).subscribe();
verify(findPublisher, never()).projection(any()); verify(findPublisher, never()).projection(any());
@ -414,7 +414,7 @@ public class ReactiveMongoTemplateUnitTests {
@Test // DATAMONGO-1719, DATAMONGO-2041 @Test // DATAMONGO-1719, DATAMONGO-2041
void appliesFieldsToDtoProjection() { 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(); Jedi.class, FindPublisherPreparer.NO_OP_PREPARER).subscribe();
verify(findPublisher).projection(eq(new Document("firstname", 1))); verify(findPublisher).projection(eq(new Document("firstname", 1)));
@ -423,7 +423,7 @@ public class ReactiveMongoTemplateUnitTests {
@Test // DATAMONGO-1719 @Test // DATAMONGO-1719
void doesNotApplyFieldsToDtoProjectionWhenQueryDefinesFields() { 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(); Jedi.class, FindPublisherPreparer.NO_OP_PREPARER).subscribe();
verify(findPublisher).projection(eq(new Document("bar", 1))); verify(findPublisher).projection(eq(new Document("bar", 1)));
@ -432,7 +432,7 @@ public class ReactiveMongoTemplateUnitTests {
@Test // DATAMONGO-1719 @Test // DATAMONGO-1719
void doesNotApplyFieldsWhenTargetIsNotAProjection() { 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(); Person.class, FindPublisherPreparer.NO_OP_PREPARER).subscribe();
verify(findPublisher, never()).projection(any()); verify(findPublisher, never()).projection(any());
@ -441,7 +441,7 @@ public class ReactiveMongoTemplateUnitTests {
@Test // DATAMONGO-1719 @Test // DATAMONGO-1719
void doesNotApplyFieldsWhenTargetExtendsDomainType() { 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(); PersonExtended.class, FindPublisherPreparer.NO_OP_PREPARER).subscribe();
verify(findPublisher, never()).projection(any()); verify(findPublisher, never()).projection(any());

Loading…
Cancel
Save