From b90bb23f27247b0928119d9c68bd886ef1bcb671 Mon Sep 17 00:00:00 2001 From: Oscar Morales Date: Mon, 19 May 2025 14:12:32 -0600 Subject: [PATCH] Add FindOnePipelineAsync method --- .../Contracts/ICollectionRepository.cs | 8 ++++++++ .../Repositories/CollectionRepository.cs | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/Core.Blueprint.Mongo/Contracts/ICollectionRepository.cs b/Core.Blueprint.Mongo/Contracts/ICollectionRepository.cs index 644452d..92dd967 100644 --- a/Core.Blueprint.Mongo/Contracts/ICollectionRepository.cs +++ b/Core.Blueprint.Mongo/Contracts/ICollectionRepository.cs @@ -148,5 +148,13 @@ namespace Core.Blueprint.Mongo /// An expression used to filter the documents to delete. /// A representing the asynchronous operation. Task DeleteManyAsync(Expression> filterExpression); + + /// + /// Executes an aggregation pipeline and returns the first document in the result asynchronously. + /// + /// The type of the output document you expect from the pipeline. + /// The aggregation pipeline definition to execute. + /// The first document from the aggregation result, or null if none found. + Task FindOnePipelineAsync(PipelineDefinition pipeline); } } diff --git a/Core.Blueprint.Mongo/Repositories/CollectionRepository.cs b/Core.Blueprint.Mongo/Repositories/CollectionRepository.cs index 9214251..3f5f6a0 100644 --- a/Core.Blueprint.Mongo/Repositories/CollectionRepository.cs +++ b/Core.Blueprint.Mongo/Repositories/CollectionRepository.cs @@ -248,5 +248,16 @@ namespace Core.Blueprint.Mongo { return Task.Run(() => _collection.DeleteManyAsync(filterExpression)); } + + /// + /// Executes an aggregation pipeline and returns the first document in the result asynchronously. + /// + /// The type of the output document you expect from the pipeline. + /// The aggregation pipeline definition to execute. + /// The first document from the aggregation result, or null if none found. + public virtual Task FindOnePipelineAsync(PipelineDefinition pipeline) + { + return Task.Run(() => _collection.Aggregate(pipeline).FirstOrDefaultAsync()); + } } }