@@ -444,6 +444,123 @@ public DataResult update(ClientSession clientSession, List<? extends Bson> queri
444444 start );
445445 }
446446
447+ /**
448+ * Update documents using an aggregation pipeline.
449+ *
450+ * @param query the filter to select the documents to update
451+ * @param pipeline the aggregation pipeline to apply for the update
452+ * @param options additional options for the query and update operation
453+ * @return a DataResult containing the result of the update operation
454+ */
455+ public DataResult updateWithPipeline (Bson query , List <? extends Bson > pipeline , QueryOptions options ) {
456+ return updateWithPipeline (null , query , pipeline , options );
457+ }
458+
459+ /**
460+ * Update documents using an aggregation pipeline.
461+ *
462+ * @param clientSession the client session to use for the operation, or null if not using sessions
463+ * @param query the filter to select the documents to update
464+ * @param pipeline the aggregation pipeline to apply for the update
465+ * @param options additional options for the query and update operation
466+ * @return a DataResult containing the result of the update operation
467+ */
468+ public DataResult updateWithPipeline (ClientSession clientSession , Bson query , List <? extends Bson > pipeline , QueryOptions options ) {
469+ long start = startQuery ();
470+
471+ boolean upsert = false ;
472+ boolean multi = false ;
473+ if (options != null ) {
474+ upsert = options .getBoolean (UPSERT );
475+ multi = options .getBoolean (MULTI );
476+ }
477+
478+ UpdateResult updateResult = mongoDBNativeQuery .updateWithPipeline (clientSession , query , pipeline , upsert , multi );
479+ return endWrite (updateResult .getMatchedCount (), updateResult .getUpsertedId () != null ? 1 : 0 ,
480+ updateResult .getUpsertedId () == null ? updateResult .getModifiedCount () : 0 , 0 , 0 , start );
481+ }
482+
483+ /**
484+ * Finds a document matching the given query, applies an aggregation pipeline to update it, and returns the updated document.
485+ *
486+ * @param query the filter to select the document to update
487+ * @param projection the fields to return in the resulting document
488+ * @param sort the sort criteria to apply before finding the document
489+ * @param pipeline the aggregation pipeline to apply for the update
490+ * @param options additional options for the query and update operation
491+ * @return a DataResult containing the updated document, or an empty result if no document matched
492+ */
493+ public DataResult <Document > findAndUpdateWithPipeline (Bson query , Bson projection , Bson sort ,
494+ List <? extends Bson > pipeline , QueryOptions options ) {
495+ return privateFindAndUpdateWithPipeline (null , query , projection , sort , pipeline , options , null , null );
496+ }
497+
498+ /**
499+ * Finds a document matching the given query, applies an aggregation pipeline to update it, and returns the updated document.
500+ *
501+ * @param clientSession the client session to use for the operation, or null if not using sessions
502+ * @param query the filter to select the document to update
503+ * @param projection the fields to return in the resulting document
504+ * @param sort the sort criteria to apply before finding the document
505+ * @param pipeline the aggregation pipeline to apply for the update
506+ * @param options additional options for the query and update operation
507+ * @return a DataResult containing the updated document, or an empty result if no document matched
508+ */
509+ public DataResult <Document > findAndUpdateWithPipeline (ClientSession clientSession , Bson query , Bson projection , Bson sort ,
510+ List <? extends Bson > pipeline , QueryOptions options ) {
511+ return privateFindAndUpdateWithPipeline (clientSession , query , projection , sort , pipeline , options , null , null );
512+ }
513+
514+ /**
515+ * Finds a document matching the given query, applies an aggregation pipeline to update it, and returns the updated document.
516+ *
517+ * @param query the filter to select the document to update
518+ * @param projection the fields to return in the resulting document
519+ * @param sort the sort criteria to apply before finding the document
520+ * @param pipeline the aggregation pipeline to apply for the update
521+ * @param clazz the class type to convert the result to; if null or Document.class, returns a Document
522+ * @param options additional options for the query and update operation
523+ * @param <T> the type of the returned result
524+ * @return a DataResult containing the updated document, or an empty result if no document matched
525+ */
526+ public <T > DataResult <T > findAndUpdateWithPipeline (Bson query , Bson projection , Bson sort ,
527+ List <? extends Bson > pipeline , Class <T > clazz , QueryOptions options ) {
528+ return privateFindAndUpdateWithPipeline (null , query , projection , sort , pipeline , options , clazz , null );
529+ }
530+
531+ /**
532+ * Finds a document matching the given query, applies an aggregation pipeline to update it, and returns the updated document.
533+ *
534+ * @param clientSession the client session to use for the operation, or null if not using sessions
535+ * @param query the filter to select the document to update
536+ * @param projection the fields to return in the resulting document
537+ * @param sort the sort criteria to apply before finding the document
538+ * @param pipeline the aggregation pipeline to apply for the update
539+ * @param clazz the class type to convert the result to; if null or Document.class, returns a Document
540+ * @param options additional options for the query and update operation
541+ * @param <T> the type of the returned result
542+ * @return a DataResult containing the updated document, or an empty result if no document matched
543+ */
544+ public <T > DataResult <T > findAndUpdateWithPipeline (ClientSession clientSession , Bson query , Bson projection , Bson sort ,
545+ List <? extends Bson > pipeline , Class <T > clazz , QueryOptions options ) {
546+ return privateFindAndUpdateWithPipeline (clientSession , query , projection , sort , pipeline , options , clazz , null );
547+ }
548+
549+ private <T > DataResult <T > privateFindAndUpdateWithPipeline (ClientSession clientSession , Bson query , Bson projection , Bson sort ,
550+ List <? extends Bson > pipeline , QueryOptions options , Class <T > clazz ,
551+ ComplexTypeConverter <T , Document > converter ) {
552+ long start = startQuery ();
553+ Document result = mongoDBNativeQuery .findAndUpdateWithPipeline (clientSession , query , projection , sort , pipeline , options );
554+ if (clazz != null && !clazz .equals (Document .class )) {
555+ try {
556+ return endQuery (Collections .singletonList (objectMapper .readValue (objectWriter .writeValueAsString (result ), clazz )), start );
557+ } catch (IOException e ) {
558+ logger .error ("Error deserializing result: " + e .getMessage (), e );
559+ }
560+ }
561+ return endQuery (Collections .singletonList (result ), start );
562+ }
563+
447564 public DataResult remove (Bson query , QueryOptions options ) {
448565 return remove (null , query , options );
449566 }
0 commit comments