113
113
import com .mongodb .client .result .UpdateResult ;
114
114
115
115
/**
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}.
117
133
*
118
134
* @author Thomas Risberg
119
135
* @author Graeme Rocher
@@ -778,7 +794,7 @@ public <T> T findOne(Query query, Class<T> entityClass, String collectionName) {
778
794
779
795
if (ObjectUtils .isEmpty (query .getSortObject ())) {
780
796
781
- return doFindOne (createDelegate (query ), collectionName , query .getQueryObject (), query .getFieldsObject (),
797
+ return doFindOne (collectionName , createDelegate (query ), query .getQueryObject (), query .getFieldsObject (),
782
798
new QueryCursorPreparer (query , entityClass ), entityClass );
783
799
} else {
784
800
query .limit (1 );
@@ -827,7 +843,7 @@ public <T> List<T> find(Query query, Class<T> entityClass, String collectionName
827
843
Assert .notNull (collectionName , "CollectionName must not be null" );
828
844
Assert .notNull (entityClass , "EntityClass must not be null" );
829
845
830
- return doFind (createDelegate (query ), collectionName , query .getQueryObject (), query .getFieldsObject (), entityClass ,
846
+ return doFind (collectionName , createDelegate (query ), query .getQueryObject (), query .getFieldsObject (), entityClass ,
831
847
new QueryCursorPreparer (query , entityClass ));
832
848
}
833
849
@@ -847,7 +863,7 @@ public <T> T findById(Object id, Class<T> entityClass, String collectionName) {
847
863
848
864
String idKey = operations .getIdPropertyName (entityClass );
849
865
850
- return doFindOne (CollectionPreparer .identity (), collectionName , new Document (idKey , id ), new Document (),
866
+ return doFindOne (collectionName , CollectionPreparer .identity (), new Document (idKey , id ), new Document (),
851
867
entityClass );
852
868
}
853
869
@@ -1122,8 +1138,7 @@ public long estimatedCount(String collectionName) {
1122
1138
}
1123
1139
1124
1140
protected long doEstimatedCount (CollectionPreparer <MongoCollection <Document >> collectionPreparer ,
1125
- String collectionName ,
1126
- EstimatedDocumentCountOptions options ) {
1141
+ String collectionName , EstimatedDocumentCountOptions options ) {
1127
1142
return execute (collectionName ,
1128
1143
collection -> collectionPreparer .prepare (collection ).estimatedDocumentCount (options ));
1129
1144
}
@@ -2376,33 +2391,35 @@ private CreateCollectionOptions getCreateCollectionOptions(Document document) {
2376
2391
* The query document is specified as a standard {@link Document} and so is the fields specification.
2377
2392
*
2378
2393
* @param collectionName name of the collection to retrieve the objects from.
2394
+ * @param collectionPreparer the preparer to prepare the collection for the actual use.
2379
2395
* @param query the query document that specifies the criteria used to find a record.
2380
2396
* @param fields the document that specifies the fields to be returned.
2381
2397
* @param entityClass the parameterized type of the returned list.
2382
2398
* @return the converted object or {@literal null} if none exists.
2383
2399
*/
2384
2400
@ 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 );
2388
2404
}
2389
2405
2390
2406
/**
2391
2407
* Map the results of an ad-hoc query on the default MongoDB collection to an object using the template's converter.
2392
2408
* The query document is specified as a standard {@link Document} and so is the fields specification.
2393
2409
*
2394
2410
* @param collectionName name of the collection to retrieve the objects from.
2411
+ * @param collectionPreparer the preparer to prepare the collection for the actual use.
2395
2412
* @param query the query document that specifies the criteria used to find a record.
2396
2413
* @param fields the document that specifies the fields to be returned.
2397
- * @param entityClass the parameterized type of the returned list.
2398
2414
* @param preparer the preparer used to modify the cursor on execution.
2415
+ * @param entityClass the parameterized type of the returned list.
2399
2416
* @return the converted object or {@literal null} if none exists.
2400
2417
* @since 2.2
2401
2418
*/
2402
2419
@ Nullable
2403
2420
@ 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 ) {
2406
2423
2407
2424
MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (entityClass );
2408
2425
@@ -2424,14 +2441,15 @@ protected <T> T doFindOne(CollectionPreparer collectionPreparer, String collecti
2424
2441
* query document is specified as a standard Document and so is the fields specification.
2425
2442
*
2426
2443
* @param collectionName name of the collection to retrieve the objects from
2444
+ * @param collectionPreparer the preparer to prepare the collection for the actual use.
2427
2445
* @param query the query document that specifies the criteria used to find a record
2428
2446
* @param fields the document that specifies the fields to be returned
2429
2447
* @param entityClass the parameterized type of the returned list.
2430
2448
* @return the List of converted objects.
2431
2449
*/
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 ,
2435
2453
new ReadDocumentCallback <>(this .mongoConverter , entityClass , collectionName ));
2436
2454
}
2437
2455
@@ -2441,21 +2459,23 @@ protected <T> List<T> doFind(CollectionPreparer collectionPreparer, String colle
2441
2459
* specified as a standard Document and so is the fields specification.
2442
2460
*
2443
2461
* @param collectionName name of the collection to retrieve the objects from.
2462
+ * @param collectionPreparer the preparer to prepare the collection for the actual use.
2444
2463
* @param query the query document that specifies the criteria used to find a record.
2445
2464
* @param fields the document that specifies the fields to be returned.
2446
2465
* @param entityClass the parameterized type of the returned list.
2447
2466
* @param preparer allows for customization of the {@link FindIterable} used when iterating over the result set,
2448
2467
* (apply limits, skips and so on).
2449
2468
* @return the {@link List} of converted objects.
2450
2469
*/
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 ,
2454
2473
new ReadDocumentCallback <>(mongoConverter , entityClass , collectionName ));
2455
2474
}
2456
2475
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 ) {
2459
2479
2460
2480
MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (entityClass );
2461
2481
@@ -2478,8 +2498,8 @@ protected <S, T> List<T> doFind(CollectionPreparer collectionPreparer, String co
2478
2498
*
2479
2499
* @since 2.0
2480
2500
*/
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 ) {
2483
2503
2484
2504
MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (sourceClass );
2485
2505
EntityProjection <T , S > projection = operations .introspectProjection (targetClass , sourceClass );
@@ -2900,8 +2920,7 @@ private static class FindCallback implements CollectionCallback<FindIterable<Doc
2900
2920
private final @ Nullable com .mongodb .client .model .Collation collation ;
2901
2921
2902
2922
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 ) {
2905
2924
2906
2925
Assert .notNull (query , "Query must not be null" );
2907
2926
Assert .notNull (fields , "Fields must not be null" );
@@ -2970,8 +2989,7 @@ private static class FindAndRemoveCallback implements CollectionCallback<Documen
2970
2989
private final Optional <Collation > collation ;
2971
2990
2972
2991
FindAndRemoveCallback (CollectionPreparer <MongoCollection <Document >> collectionPreparer , Document query ,
2973
- Document fields , Document sort ,
2974
- @ Nullable Collation collation ) {
2992
+ Document fields , Document sort , @ Nullable Collation collation ) {
2975
2993
this .collectionPreparer = collectionPreparer ;
2976
2994
2977
2995
this .query = query ;
@@ -3001,8 +3019,7 @@ private static class FindAndModifyCallback implements CollectionCallback<Documen
3001
3019
private final FindAndModifyOptions options ;
3002
3020
3003
3021
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 ) {
3006
3023
3007
3024
this .collectionPreparer = collectionPreparer ;
3008
3025
this .query = query ;
@@ -3060,8 +3077,8 @@ private static class FindAndReplaceCallback implements CollectionCallback<Docume
3060
3077
private final FindAndReplaceOptions options ;
3061
3078
3062
3079
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 ) {
3065
3082
this .collectionPreparer = collectionPreparer ;
3066
3083
this .query = query ;
3067
3084
this .fields = fields ;
0 commit comments