Te damos la bienvenida a nuestro espacio, aquí encontrarás la resolución a lo que buscabas.
Ejemplo: paginación a través del resultado de mongodb en C #
publicclassPerson[BsonId][BsonRepresentation(BsonType.String)]publicstring Id get;set;publicstring FirstName get;set;publicstring Surname get;set;publicclassPegerpublicint Count get;set;publicint Page get;set;publicint Size get;set;publicIEnumerable<Person> Items get;set;classProgramstaticasyncTaskMain(string[] args)var client =newMongoClient();var database = client.GetDatabase("pager_test");var collection = database.GetCollection<Person>(nameof(Person));int page =1;int pageSize =5;var results =awaitGetPagerResultAsync(page, pageSize, collection);privatestaticasyncTask<Peger>GetPagerResultAsync(int page,int pageSize,IMongoCollection<Person> collection)// count facet, aggregation stage of countvar countFacet = AggregateFacet.Create("countFacet",
PipelineDefinition<Person, AggregateCountResult>.Create(new[]
PipelineStageDefinitionBuilder.Count<Person>()));// data facet, we’ll use this to sort the data and do the skip and limiting of the results for the paging.var dataFacet = AggregateFacet.Create("dataFacet",
PipelineDefinition<Person, Person>.Create(new[]
PipelineStageDefinitionBuilder.Sort(Builders<Person>.Sort.Ascending(x => x.Surname)),
PipelineStageDefinitionBuilder.Skip<Person>((page -1)* pageSize),
PipelineStageDefinitionBuilder.Limit<Person>(pageSize),));var filter = Builders<Person>.Filter.Empty;var aggregation =await collection.Aggregate().Match(filter).Facet(countFacet, dataFacet).ToListAsync();var count = aggregation.First().Facets.First(x => x.Name =="countFacet").Output<AggregateCountResult>()?.FirstOrDefault()?.Count ??0;var data = aggregation.First().Facets.First(x => x.Name =="dataFacet").Output<Person>();returnnewPager
Count =(int)count / pageSize,
Size = pageSize,
Page = page,
Items = data
;
Si crees que te ha sido provechoso este artículo, te agradeceríamos que lo compartas con más programadores de esta forma nos ayudas a dar difusión a nuestro contenido.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)