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()); + } } }