我已经使用Aggregate方法实现了分组,我想为每个组实现分页。当我在Group中调用Skip函数时,我的代码给出了错误。
我实现:
var queryResult = await collection.Aggregate().Match(expr).Sort(sort)
.Group(
x => x.ApprovalStatus.Value,
g => new
{
approvalStatus = g.Key,
data = g.Select(f => new FtagDashboardDTO
{
Id = f.Id,
FtagId = f.FtagId,
Title = f.Title,
Assignee = f.Assignee,
ApprovalStatus = f.ApprovalStatus,
Priority = f.Priority,
DueDate = f.DueDate
}).Take(5).ToList()
})
.ToListAsync();
误差
Specified method is not supported.:: StackTrace :: at MongoDB.Driver.Linq.Linq2Implementation.Processors.AccumulatorBinder.GetAccumulatorArgument(Expression node)rn at MongoDB.Driver.Linq.Linq2Implementation.Processors.AccumulatorBinder.TryGetAccumulatorTypeAndArgument(PipelineExpression node, AccumulatorType& accumulatorType, Expression& argument)rn at MongoDB.Driver.Linq.Linq2Implementation.Processors.AccumulatorBinder.VisitPipeline(PipelineExpression node)rn at MongoDB.Driver.Linq.Linq2Implementation.Processors.EmbeddedPipeline.EmbeddedPipelineBinder.Bind(Expression node, IBindingContext parent)rn at MongoDB.Driver.Linq.Linq2Implementation.Processors.SerializationBinder.BindEmbeddedPipeline(MethodCallExpression node)rn at MongoDB.Driver.Linq.Linq2Implementation.Processors.SerializationBinder.VisitMethodCall(MethodCallExpression node)rn at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)rn at MongoDB.Driver.Linq.Linq2Implementation.Processors.SerializationBinder.Visit(Expression node)rn at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)rn at System.Linq.Expressions.ExpressionVisitor.VisitNew(NewExpression node)rn at System.Linq.Expressions.NewExpression.Accept(ExpressionVisitor visitor)rn at MongoDB.Driver.Linq.Linq2Implementation.Translators.AggregateGroupTranslator.BindGroup[TKey,TDocument,TResult](PipelineBindingContext bindingContext, Expression`1 groupProjector, IBsonSerializer`1 parameterSerializer, Expression keySelector)rn at MongoDB.Driver.Linq.Linq2Implementation.Translators.AggregateGroupTranslator.Translate[TKey,TDocument,TResult](Expression`1 idProjector, Expression`1 groupProjector, IBsonSerializer`1 parameterSerializer, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions)rn at MongoDB.Driver.GroupExpressionProjection`3.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)rn at MongoDB.Driver.PipelineStageDefinitionBuilder.<>c__DisplayClass19_0`2.<Group>b__0(IBsonSerializer`1 s, IBsonSerializerRegistry sr, LinqProvider linqProvider)rn at MongoDB.Driver.AppendedStagePipelineDefinition`3.Render(IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)rn at MongoDB.Driver.MongoCollectionImpl`1.AggregateAsync[TResult](IClientSessionHandle session, PipelineDefinition`2 pipeline, AggregateOptions options, CancellationToken cancellationToken)rn at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)rn at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)rn at ProfessionalMaintenance.API.Infrastructure.Repostiories.FtagRepository.GetKanbanByQuery(FtagDashboardFilterDTO filter) in C:\development\leansuite\lean-suggestion-backend\src\Services\ProfessionalMaintenance\ProfessionalMaintenance.API\Infrastructure\Repostiories\FtagRepository.cs:line 263rn at ProfessionalMaintenance.API.Controllers.FtagApiController.GetDashboard(FtagDashboardFilterDTO request) in C:\development\leansuite\lean-suggestion-backend\src\Services\ProfessionalMaintenance\ProfessionalMaintenance.API\Controllers\FtagApiController.cs:line 183rn at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)rn at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)rn at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)rn at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)rn at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)rn at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)rn at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|26_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
p。只有当我调用Take()
我不得不使用基于这个答案的聚合框架
BsonArray pipeline = new BsonArray
{ BsonDocument.Parse("{ $match: { $expr: { $eq: ['$ApprovalStatus.Name', '$$c']} } }"),
BsonDocument.Parse("{ $sort: { Id: -1 } }"),
new BsonDocument("$skip", skip),
new BsonDocument("$limit", pageSize),
};
BsonDocument lookup = new BsonDocument("$lookup",
new BsonDocument("from", "ftags")
.Add("let", new BsonDocument("c", "$_id"))
.Add("as", "Items")
.Add("pipeline", pipeline)
);
var aggregateFluent = collection.Aggregate().Match(expr).Sort(sort)
.Group(BsonDocument.Parse("{ _id: '$ApprovalStatus.Name' }"))
.AppendStage<object>(lookup)
.Project<FtagItemsGroup>("{ _id: 0, GroupName: '$_id', Items: 1 }");
var ftagItemsGroups = aggregateFluent.ToList();