181
181
* @author Bartłomiej Mazur
182
182
* @author Michael Krog
183
183
* @author Jakub Zurawa
184
+ * @author Kinjarapu Sriram
184
185
*/
185
186
public class MongoTemplate
186
187
implements MongoOperations , ApplicationContextAware , IndexOperationsProvider , ReadPreferenceAware {
@@ -1088,7 +1089,7 @@ public <T> T findAndModify(Query query, UpdateDefinition update, FindAndModifyOp
1088
1089
operations .forType (entityClass ).getCollation (query ).ifPresent (optionsToUse ::collation );
1089
1090
}
1090
1091
1091
- return doFindAndModify (createDelegate (query ), collectionName , query . getQueryObject () , query .getFieldsObject (),
1092
+ return doFindAndModify (createDelegate (query ), collectionName , query , query .getFieldsObject (),
1092
1093
getMappedSortObject (query , entityClass ), entityClass , update , optionsToUse );
1093
1094
}
1094
1095
@@ -2718,7 +2719,7 @@ protected <T> T doFindAndRemove(CollectionPreparer collectionPreparer, String co
2718
2719
}
2719
2720
2720
2721
@ SuppressWarnings ("ConstantConditions" )
2721
- protected <T > T doFindAndModify (CollectionPreparer collectionPreparer , String collectionName , Document query ,
2722
+ protected <T > T doFindAndModify (CollectionPreparer collectionPreparer , String collectionName , Query query ,
2722
2723
Document fields , Document sort , Class <T > entityClass , UpdateDefinition update ,
2723
2724
@ Nullable FindAndModifyOptions options ) {
2724
2725
@@ -2728,13 +2729,35 @@ protected <T> T doFindAndModify(CollectionPreparer collectionPreparer, String co
2728
2729
2729
2730
MongoPersistentEntity <?> entity = mappingContext .getPersistentEntity (entityClass );
2730
2731
2731
- UpdateContext updateContext = queryOperations .updateSingleContext (update , query , false );
2732
+ UpdateContext updateContext = queryOperations .updateSingleContext (update , query . getQueryObject () , false );
2732
2733
updateContext .increaseVersionForUpdateIfNecessary (entity );
2733
2734
2734
2735
Document mappedQuery = updateContext .getMappedQuery (entity );
2735
2736
Object mappedUpdate = updateContext .isAggregationUpdate () ? updateContext .getUpdatePipeline (entityClass )
2736
2737
: updateContext .getMappedUpdate (entity );
2737
2738
2739
+ FindOneAndUpdateOptions opts = new FindOneAndUpdateOptions ();
2740
+ opts .sort (sort );
2741
+ if (options .isUpsert ()) {
2742
+ opts .upsert (true );
2743
+ }
2744
+ opts .projection (fields );
2745
+ if (options .isReturnNew ()) {
2746
+ opts .returnDocument (ReturnDocument .AFTER );
2747
+ }
2748
+ options .getCollation ().map (Collation ::toMongoCollation ).ifPresent (opts ::collation );
2749
+ List <Document > arrayFilters = update .getArrayFilters ().stream ().map (ArrayFilter ::asDocument ).collect (Collectors .toList ());
2750
+ if (!arrayFilters .isEmpty ()) {
2751
+ opts .arrayFilters (arrayFilters );
2752
+ }
2753
+ Meta meta = query .getMeta ();
2754
+ if (meta .hasValues ()) {
2755
+
2756
+ if (meta .hasMaxTime ()) {
2757
+ opts .maxTime (meta .getRequiredMaxTimeMsec (), TimeUnit .MILLISECONDS );
2758
+ }
2759
+ }
2760
+
2738
2761
if (LOGGER .isDebugEnabled ()) {
2739
2762
LOGGER .debug (String .format (
2740
2763
"findAndModify using query: %s fields: %s sort: %s for class: %s and update: %s in collection: %s" ,
@@ -2744,8 +2767,7 @@ protected <T> T doFindAndModify(CollectionPreparer collectionPreparer, String co
2744
2767
}
2745
2768
2746
2769
return executeFindOneInternal (
2747
- new FindAndModifyCallback (collectionPreparer , mappedQuery , fields , sort , mappedUpdate ,
2748
- update .getArrayFilters ().stream ().map (ArrayFilter ::asDocument ).collect (Collectors .toList ()), options ),
2770
+ new FindAndModifyCallback (collectionPreparer , mappedQuery , mappedUpdate , opts ),
2749
2771
new ReadDocumentCallback <>(this .mongoConverter , entityClass , collectionName ), collectionName );
2750
2772
}
2751
2773
@@ -3157,47 +3179,25 @@ private static class FindAndModifyCallback implements CollectionCallback<Documen
3157
3179
3158
3180
private final CollectionPreparer <MongoCollection <Document >> collectionPreparer ;
3159
3181
private final Document query ;
3160
- private final Document fields ;
3161
- private final Document sort ;
3162
3182
private final Object update ;
3163
- private final List <Document > arrayFilters ;
3164
- private final FindAndModifyOptions options ;
3183
+ private final FindOneAndUpdateOptions options ;
3165
3184
3166
3185
FindAndModifyCallback (CollectionPreparer <MongoCollection <Document >> collectionPreparer , Document query ,
3167
- Document fields , Document sort , Object update , List < Document > arrayFilters , FindAndModifyOptions options ) {
3186
+ Object update , FindOneAndUpdateOptions options ) {
3168
3187
3169
3188
this .collectionPreparer = collectionPreparer ;
3170
3189
this .query = query ;
3171
- this .fields = fields ;
3172
- this .sort = sort ;
3173
3190
this .update = update ;
3174
- this .arrayFilters = arrayFilters ;
3175
3191
this .options = options ;
3176
3192
}
3177
3193
3178
3194
@ Override
3179
3195
public Document doInCollection (MongoCollection <Document > collection ) throws MongoException , DataAccessException {
3180
3196
3181
- FindOneAndUpdateOptions opts = new FindOneAndUpdateOptions ();
3182
- opts .sort (sort );
3183
- if (options .isUpsert ()) {
3184
- opts .upsert (true );
3185
- }
3186
- opts .projection (fields );
3187
- if (options .isReturnNew ()) {
3188
- opts .returnDocument (ReturnDocument .AFTER );
3189
- }
3190
-
3191
- options .getCollation ().map (Collation ::toMongoCollation ).ifPresent (opts ::collation );
3192
-
3193
- if (!arrayFilters .isEmpty ()) {
3194
- opts .arrayFilters (arrayFilters );
3195
- }
3196
-
3197
3197
if (update instanceof Document document ) {
3198
- return collectionPreparer .prepare (collection ).findOneAndUpdate (query , document , opts );
3198
+ return collectionPreparer .prepare (collection ).findOneAndUpdate (query , document , options );
3199
3199
} else if (update instanceof List ) {
3200
- return collectionPreparer .prepare (collection ).findOneAndUpdate (query , (List <Document >) update , opts );
3200
+ return collectionPreparer .prepare (collection ).findOneAndUpdate (query , (List <Document >) update , options );
3201
3201
}
3202
3202
3203
3203
throw new IllegalArgumentException (String .format ("Using %s is not supported in findOneAndUpdate" , update ));
0 commit comments