Skip to content

Commit e56f6ce

Browse files
mp911dechristophstrobl
authored andcommitted
Polishing.
Documentation, refine parameter ordering. Original Pull Request: #4288
1 parent c5c6fc1 commit e56f6ce

File tree

6 files changed

+116
-79
lines changed

6 files changed

+116
-79
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

+47-30
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,23 @@
113113
import com.mongodb.client.result.UpdateResult;
114114

115115
/**
116-
* Primary implementation of {@link MongoOperations}.
116+
* Primary implementation of {@link MongoOperations}. It simplifies the use of imperative MongoDB usage and helps to
117+
* avoid common errors. It executes core MongoDB workflow, leaving application code to provide {@link Document} and
118+
* extract results. This class executes BSON queries or updates, initiating iteration over {@link FindIterable} and
119+
* catching MongoDB exceptions and translating them to the generic, more informative exception hierarchy defined in the
120+
* org.springframework.dao package. Can be used within a service implementation via direct instantiation with a
121+
* {@link MongoDatabaseFactory} reference, or get prepared in an application context and given to services as bean
122+
* reference.
123+
* <p>
124+
* Note: The {@link MongoDatabaseFactory} should always be configured as a bean in the application context, in the first
125+
* case given to the service directly, in the second case to the prepared template.
126+
* <h3>{@link ReadPreference} and {@link com.mongodb.ReadConcern}</h3>
127+
* <p>
128+
* {@code ReadPreference} and {@code ReadConcern} are generally considered from {@link Query} and
129+
* {@link AggregationOptions} objects for the action to be executed on a particular {@link MongoCollection}.
130+
* <p>
131+
* You can also set the default {@link #setReadPreference(ReadPreference) ReadPreference} on the template level to
132+
* generally apply a {@link ReadPreference}.
117133
*
118134
* @author Thomas Risberg
119135
* @author Graeme Rocher
@@ -778,7 +794,7 @@ public <T> T findOne(Query query, Class<T> entityClass, String collectionName) {
778794

779795
if (ObjectUtils.isEmpty(query.getSortObject())) {
780796

781-
return doFindOne(createDelegate(query), collectionName, query.getQueryObject(), query.getFieldsObject(),
797+
return doFindOne(collectionName, createDelegate(query), query.getQueryObject(), query.getFieldsObject(),
782798
new QueryCursorPreparer(query, entityClass), entityClass);
783799
} else {
784800
query.limit(1);
@@ -827,7 +843,7 @@ public <T> List<T> find(Query query, Class<T> entityClass, String collectionName
827843
Assert.notNull(collectionName, "CollectionName must not be null");
828844
Assert.notNull(entityClass, "EntityClass must not be null");
829845

830-
return doFind(createDelegate(query), collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass,
846+
return doFind(collectionName, createDelegate(query), query.getQueryObject(), query.getFieldsObject(), entityClass,
831847
new QueryCursorPreparer(query, entityClass));
832848
}
833849

@@ -847,7 +863,7 @@ public <T> T findById(Object id, Class<T> entityClass, String collectionName) {
847863

848864
String idKey = operations.getIdPropertyName(entityClass);
849865

850-
return doFindOne(CollectionPreparer.identity(), collectionName, new Document(idKey, id), new Document(),
866+
return doFindOne(collectionName, CollectionPreparer.identity(), new Document(idKey, id), new Document(),
851867
entityClass);
852868
}
853869

@@ -1122,8 +1138,7 @@ public long estimatedCount(String collectionName) {
11221138
}
11231139

11241140
protected long doEstimatedCount(CollectionPreparer<MongoCollection<Document>> collectionPreparer,
1125-
String collectionName,
1126-
EstimatedDocumentCountOptions options) {
1141+
String collectionName, EstimatedDocumentCountOptions options) {
11271142
return execute(collectionName,
11281143
collection -> collectionPreparer.prepare(collection).estimatedDocumentCount(options));
11291144
}
@@ -2376,33 +2391,35 @@ private CreateCollectionOptions getCreateCollectionOptions(Document document) {
23762391
* The query document is specified as a standard {@link Document} and so is the fields specification.
23772392
*
23782393
* @param collectionName name of the collection to retrieve the objects from.
2394+
* @param collectionPreparer the preparer to prepare the collection for the actual use.
23792395
* @param query the query document that specifies the criteria used to find a record.
23802396
* @param fields the document that specifies the fields to be returned.
23812397
* @param entityClass the parameterized type of the returned list.
23822398
* @return the converted object or {@literal null} if none exists.
23832399
*/
23842400
@Nullable
2385-
protected <T> T doFindOne(CollectionPreparer collectionPreparer, String collectionName, Document query,
2386-
Document fields, Class<T> entityClass) {
2387-
return doFindOne(collectionPreparer, collectionName, query, fields, CursorPreparer.NO_OP_PREPARER, entityClass);
2401+
protected <T> T doFindOne(String collectionName, CollectionPreparer<MongoCollection<Document>> collectionPreparer,
2402+
Document query, Document fields, Class<T> entityClass) {
2403+
return doFindOne(collectionName, collectionPreparer, query, fields, CursorPreparer.NO_OP_PREPARER, entityClass);
23882404
}
23892405

23902406
/**
23912407
* Map the results of an ad-hoc query on the default MongoDB collection to an object using the template's converter.
23922408
* The query document is specified as a standard {@link Document} and so is the fields specification.
23932409
*
23942410
* @param collectionName name of the collection to retrieve the objects from.
2411+
* @param collectionPreparer the preparer to prepare the collection for the actual use.
23952412
* @param query the query document that specifies the criteria used to find a record.
23962413
* @param fields the document that specifies the fields to be returned.
2397-
* @param entityClass the parameterized type of the returned list.
23982414
* @param preparer the preparer used to modify the cursor on execution.
2415+
* @param entityClass the parameterized type of the returned list.
23992416
* @return the converted object or {@literal null} if none exists.
24002417
* @since 2.2
24012418
*/
24022419
@Nullable
24032420
@SuppressWarnings("ConstantConditions")
2404-
protected <T> T doFindOne(CollectionPreparer collectionPreparer, String collectionName, Document query,
2405-
Document fields, CursorPreparer preparer, Class<T> entityClass) {
2421+
protected <T> T doFindOne(String collectionName, CollectionPreparer<MongoCollection<Document>> collectionPreparer,
2422+
Document query, Document fields, CursorPreparer preparer, Class<T> entityClass) {
24062423

24072424
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
24082425

@@ -2424,14 +2441,15 @@ protected <T> T doFindOne(CollectionPreparer collectionPreparer, String collecti
24242441
* query document is specified as a standard Document and so is the fields specification.
24252442
*
24262443
* @param collectionName name of the collection to retrieve the objects from
2444+
* @param collectionPreparer the preparer to prepare the collection for the actual use.
24272445
* @param query the query document that specifies the criteria used to find a record
24282446
* @param fields the document that specifies the fields to be returned
24292447
* @param entityClass the parameterized type of the returned list.
24302448
* @return the List of converted objects.
24312449
*/
2432-
protected <T> List<T> doFind(CollectionPreparer collectionPreparer, String collectionName, Document query,
2433-
Document fields, Class<T> entityClass) {
2434-
return doFind(collectionPreparer, collectionName, query, fields, entityClass, null,
2450+
protected <T> List<T> doFind(String collectionName, CollectionPreparer<MongoCollection<Document>> collectionPreparer,
2451+
Document query, Document fields, Class<T> entityClass) {
2452+
return doFind(collectionName, collectionPreparer, query, fields, entityClass, null,
24352453
new ReadDocumentCallback<>(this.mongoConverter, entityClass, collectionName));
24362454
}
24372455

@@ -2441,21 +2459,23 @@ protected <T> List<T> doFind(CollectionPreparer collectionPreparer, String colle
24412459
* specified as a standard Document and so is the fields specification.
24422460
*
24432461
* @param collectionName name of the collection to retrieve the objects from.
2462+
* @param collectionPreparer the preparer to prepare the collection for the actual use.
24442463
* @param query the query document that specifies the criteria used to find a record.
24452464
* @param fields the document that specifies the fields to be returned.
24462465
* @param entityClass the parameterized type of the returned list.
24472466
* @param preparer allows for customization of the {@link FindIterable} used when iterating over the result set,
24482467
* (apply limits, skips and so on).
24492468
* @return the {@link List} of converted objects.
24502469
*/
2451-
protected <T> List<T> doFind(CollectionPreparer collectionPreparer, String collectionName, Document query,
2452-
Document fields, Class<T> entityClass, CursorPreparer preparer) {
2453-
return doFind(collectionPreparer, collectionName, query, fields, entityClass, preparer,
2470+
protected <T> List<T> doFind(String collectionName, CollectionPreparer<MongoCollection<Document>> collectionPreparer,
2471+
Document query, Document fields, Class<T> entityClass, CursorPreparer preparer) {
2472+
return doFind(collectionName, collectionPreparer, query, fields, entityClass, preparer,
24542473
new ReadDocumentCallback<>(mongoConverter, entityClass, collectionName));
24552474
}
24562475

2457-
protected <S, T> List<T> doFind(CollectionPreparer collectionPreparer, String collectionName, Document query,
2458-
Document fields, Class<S> entityClass, @Nullable CursorPreparer preparer, DocumentCallback<T> objectCallback) {
2476+
protected <S, T> List<T> doFind(String collectionName,
2477+
CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query, Document fields,
2478+
Class<S> entityClass, @Nullable CursorPreparer preparer, DocumentCallback<T> objectCallback) {
24592479

24602480
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
24612481

@@ -2478,8 +2498,8 @@ protected <S, T> List<T> doFind(CollectionPreparer collectionPreparer, String co
24782498
*
24792499
* @since 2.0
24802500
*/
2481-
<S, T> List<T> doFind(CollectionPreparer collectionPreparer, String collectionName, Document query, Document fields,
2482-
Class<S> sourceClass, Class<T> targetClass, CursorPreparer preparer) {
2501+
<S, T> List<T> doFind(CollectionPreparer<MongoCollection<Document>> collectionPreparer, String collectionName,
2502+
Document query, Document fields, Class<S> sourceClass, Class<T> targetClass, CursorPreparer preparer) {
24832503

24842504
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(sourceClass);
24852505
EntityProjection<T, S> projection = operations.introspectProjection(targetClass, sourceClass);
@@ -2900,8 +2920,7 @@ private static class FindCallback implements CollectionCallback<FindIterable<Doc
29002920
private final @Nullable com.mongodb.client.model.Collation collation;
29012921

29022922
public FindCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query,
2903-
Document fields,
2904-
@Nullable com.mongodb.client.model.Collation collation) {
2923+
Document fields, @Nullable com.mongodb.client.model.Collation collation) {
29052924

29062925
Assert.notNull(query, "Query must not be null");
29072926
Assert.notNull(fields, "Fields must not be null");
@@ -2970,8 +2989,7 @@ private static class FindAndRemoveCallback implements CollectionCallback<Documen
29702989
private final Optional<Collation> collation;
29712990

29722991
FindAndRemoveCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query,
2973-
Document fields, Document sort,
2974-
@Nullable Collation collation) {
2992+
Document fields, Document sort, @Nullable Collation collation) {
29752993
this.collectionPreparer = collectionPreparer;
29762994

29772995
this.query = query;
@@ -3001,8 +3019,7 @@ private static class FindAndModifyCallback implements CollectionCallback<Documen
30013019
private final FindAndModifyOptions options;
30023020

30033021
FindAndModifyCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query,
3004-
Document fields, Document sort,
3005-
Object update, List<Document> arrayFilters, FindAndModifyOptions options) {
3022+
Document fields, Document sort, Object update, List<Document> arrayFilters, FindAndModifyOptions options) {
30063023

30073024
this.collectionPreparer = collectionPreparer;
30083025
this.query = query;
@@ -3060,8 +3077,8 @@ private static class FindAndReplaceCallback implements CollectionCallback<Docume
30603077
private final FindAndReplaceOptions options;
30613078

30623079
FindAndReplaceCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query,
3063-
Document fields, Document sort,
3064-
Document update, @Nullable com.mongodb.client.model.Collation collation, FindAndReplaceOptions options) {
3080+
Document fields, Document sort, Document update, @Nullable com.mongodb.client.model.Collation collation,
3081+
FindAndReplaceOptions options) {
30653082
this.collectionPreparer = collectionPreparer;
30663083
this.query = query;
30673084
this.fields = fields;

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveFindOperationSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private Flux<T> doFind(@Nullable FindPublisherPreparer preparer) {
170170
Document queryObject = query.getQueryObject();
171171
Document fieldsObject = query.getFieldsObject();
172172

173-
return template.doFind(ReactiveCollectionPreparerDelegate.of(query), getCollectionName(), queryObject,
173+
return template.doFind(getCollectionName(), ReactiveCollectionPreparerDelegate.of(query), queryObject,
174174
fieldsObject, domainType, returnType, preparer != null ? preparer : getCursorPreparer(query));
175175
}
176176

0 commit comments

Comments
 (0)