Add FindOnePipelineAsync method

This commit is contained in:
Oscar Morales 2025-05-19 14:12:32 -06:00
parent f694b9a41a
commit b90bb23f27
2 changed files with 19 additions and 0 deletions

View File

@ -148,5 +148,13 @@ namespace Core.Blueprint.Mongo
/// <param name="filterExpression">An expression used to filter the documents to delete.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task DeleteManyAsync(Expression<Func<TDocument, bool>> filterExpression);
/// <summary>
/// Executes an aggregation pipeline and returns the first document in the result asynchronously.
/// </summary>
/// <typeparam name="TOutput">The type of the output document you expect from the pipeline.</typeparam>
/// <param name="pipeline">The aggregation pipeline definition to execute.</param>
/// <returns>The first document from the aggregation result, or null if none found.</returns>
Task<TOutput> FindOnePipelineAsync<TOutput>(PipelineDefinition<TDocument, TOutput> pipeline);
}
}

View File

@ -248,5 +248,16 @@ namespace Core.Blueprint.Mongo
{
return Task.Run(() => _collection.DeleteManyAsync(filterExpression));
}
/// <summary>
/// Executes an aggregation pipeline and returns the first document in the result asynchronously.
/// </summary>
/// <typeparam name="TOutput">The type of the output document you expect from the pipeline.</typeparam>
/// <param name="pipeline">The aggregation pipeline definition to execute.</param>
/// <returns>The first document from the aggregation result, or null if none found.</returns>
public virtual Task<TOutput> FindOnePipelineAsync<TOutput>(PipelineDefinition<TDocument, TOutput> pipeline)
{
return Task.Run(() => _collection.Aggregate(pipeline).FirstOrDefaultAsync());
}
}
}