/// <summary> /// Get page of category settings /// </summary> /// <param name="pageIndex">0-based page index</param> /// <param name="pageSize"></param> /// <param name="descending"></param> /// <returns></returns> public virtual async Task <TotalResult <List <TCategory> > > Find( int pageIndex, int pageSize, bool descending) { //select categories var pipeline = new EmptyPipelineDefinition <TDeliveryType>(); var pipeline2 = pipeline.Unwind(x => x.SubscriberCategorySettings) .As <TDeliveryType, BsonDocument, TDeliveryType>(); var pipeline3 = pipeline2.ReplaceRoot(x => x.SubscriberCategorySettings) .As <TDeliveryType, List <TCategory>, TCategory>(); //count total categories var countPipeline = new EmptyPipelineDefinition <TCategory>().Count(); var countFacetStage = AggregateFacet.Create("count", countPipeline); //limit page of categories var limitPipeline = new EmptyPipelineDefinition <TCategory>() .As <TCategory, TCategory, TCategory>(); var sort = Builders <TCategory> .Sort.Combine(); sort = descending ? sort.Descending(x => x.SubscriberCategorySettingsId) : sort.Ascending(x => x.SubscriberCategorySettingsId); limitPipeline = limitPipeline.Sort(sort); int skip = MongoDbPageNumbers.ToSkipNumber(pageIndex, pageSize); limitPipeline = limitPipeline.Skip(skip); limitPipeline = limitPipeline.Limit(pageSize); var dataFacetStage = AggregateFacet.Create("data", limitPipeline); //combine facets var pipeline4 = pipeline3.Facet(new AggregateFacet <TCategory>[] { countFacetStage, dataFacetStage }); //query IAsyncCursor <AggregateFacetResults> categorySettings = await _collectionFactory .GetCollection <TDeliveryType>() .AggregateAsync(pipeline4, new AggregateOptions()) .ConfigureAwait(false); List <AggregateFacetResults> aggregateFacetResults = await categorySettings .ToListAsync() .ConfigureAwait(false); AggregateFacetResult countFacetResult = aggregateFacetResults[0].Facets .First(x => x.Name == "count"); AggregateFacetResult dataFacetResult = aggregateFacetResults[0].Facets .First(x => x.Name == "data"); var count = countFacetResult.Output <AggregateCountResult>(); var categories = dataFacetResult.Output <TCategory>(); return(new TotalResult <List <TCategory> >( categories.ToList(), count.Count)); }
private PipelineDefinition <object, object> GetPipline() { PipelineDefinition <object, object> pipline = new EmptyPipelineDefinition <object>(); if (!string.IsNullOrWhiteSpace(SearchCondition)) { pipline = pipline.Match(MongoTable.QueryToFilter(SearchCondition)); } if (!string.IsNullOrWhiteSpace(OrderByField)) { var sortBuilder = new SortDefinitionBuilder <object>(); var sort = Ascending ? sortBuilder.Ascending(OrderByField) : sortBuilder.Descending(OrderByField); pipline = pipline.Sort(sort); } if (skipcount > 0) { pipline = pipline.Skip(skipcount); } return(pipline); }