0

I have implemented Grouping using the Aggregate method , I want to implement Pagination for each group. When I call Skip function inside Group my code gives error.

My Implementation:

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

Error

Specified method is not supported.:: StackTrace ::     at MongoDB.Driver.Linq.Linq2Implementation.Processors.AccumulatorBinder.GetAccumulatorArgument(Expression node)\r\n   at MongoDB.Driver.Linq.Linq2Implementation.Processors.AccumulatorBinder.TryGetAccumulatorTypeAndArgument(PipelineExpression node, AccumulatorType& accumulatorType, Expression& argument)\r\n   at MongoDB.Driver.Linq.Linq2Implementation.Processors.AccumulatorBinder.VisitPipeline(PipelineExpression node)\r\n   at MongoDB.Driver.Linq.Linq2Implementation.Processors.EmbeddedPipeline.EmbeddedPipelineBinder.Bind(Expression node, IBindingContext parent)\r\n   at MongoDB.Driver.Linq.Linq2Implementation.Processors.SerializationBinder.BindEmbeddedPipeline(MethodCallExpression node)\r\n   at MongoDB.Driver.Linq.Linq2Implementation.Processors.SerializationBinder.VisitMethodCall(MethodCallExpression node)\r\n   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)\r\n   at MongoDB.Driver.Linq.Linq2Implementation.Processors.SerializationBinder.Visit(Expression node)\r\n   at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitNew(NewExpression node)\r\n   at System.Linq.Expressions.NewExpression.Accept(ExpressionVisitor visitor)\r\n   at MongoDB.Driver.Linq.Linq2Implementation.Translators.AggregateGroupTranslator.BindGroup[TKey,TDocument,TResult](PipelineBindingContext bindingContext, Expression`1 groupProjector, IBsonSerializer`1 parameterSerializer, Expression keySelector)\r\n   at MongoDB.Driver.Linq.Linq2Implementation.Translators.AggregateGroupTranslator.Translate[TKey,TDocument,TResult](Expression`1 idProjector, Expression`1 groupProjector, IBsonSerializer`1 parameterSerializer, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions)\r\n   at MongoDB.Driver.GroupExpressionProjection`3.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)\r\n   at MongoDB.Driver.PipelineStageDefinitionBuilder.<>c__DisplayClass19_0`2.<Group>b__0(IBsonSerializer`1 s, IBsonSerializerRegistry sr, LinqProvider linqProvider)\r\n   at MongoDB.Driver.AppendedStagePipelineDefinition`3.Render(IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry, LinqProvider linqProvider)\r\n   at MongoDB.Driver.MongoCollectionImpl`1.AggregateAsync[TResult](IClientSessionHandle session, PipelineDefinition`2 pipeline, AggregateOptions options, CancellationToken cancellationToken)\r\n   at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)\r\n   at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)\r\n   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 263\r\n   at ProfessionalMaintenance.API.Controllers.FtagApiController.GetDashboard(FtagDashboardFilterDTO request) in C:\\development\\leansuite\\lean-suggestion-backend\\src\\Services\\ProfessionalMaintenance\\ProfessionalMaintenance.API\\Controllers\\FtagApiController.cs:line 183\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|26_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)

P.S Error only occur when I call Take()

Ans Bilal
  • 987
  • 1
  • 10
  • 28

1 Answers1

0

I had to use Aggregate framework base on this answer

        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();
Ans Bilal
  • 987
  • 1
  • 10
  • 28