Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

paging thru result from mongodb in C#

public class Person
{
    [BsonId]
    [BsonRepresentation(BsonType.String)]
    public string Id { get; set; }

    public string FirstName { get; set; }

    public string Surname { get; set; }
}

public class Peger
{
    public int Count { get; set; }

    public int Page { get; set; }

    public int Size { get; set; }

    public IEnumerable<Person> Items { get; set; }
}

class Program
{
    static async Task Main(string[] args)
    {
        var client = new MongoClient();
        var database = client.GetDatabase("pager_test");
        var collection = database.GetCollection<Person>(nameof(Person));

        int page = 1;
        int pageSize = 5;
        var results = await GetPagerResultAsync(page, pageSize, collection);
    }

    private static async Task<Peger> GetPagerResultAsync(int page, int pageSize, IMongoCollection<Person> collection)
    {
        // count facet, aggregation stage of count
        var 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>();

        return new Pager
        {
            Count = (int)count / pageSize,
            Size = pageSize,
            Page = page,
            Items = data
        };
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to delete dotnet project 
Csharp :: asp.netcore: develop on win10 run on ubuntu 
Csharp :: c# order of initialization 
Csharp :: c# function<T 
Csharp :: C# if (if-then) Statement 
Csharp :: gegenstände bewegen c# 
Csharp :: how to make a destroy reference in unity 
Csharp :: Custom Circular Picture Box C# win Form app 
Csharp :: Getting the ID of the element that fired an event 
Csharp :: lambda distinct by property 
Csharp :: C# data base sql 
Csharp :: Startup.cs file 
Csharp :: check if variable less than in f# 
Csharp :: Display the elements in an array one at a time using getkeydown in unity 
Csharp :: Adding number of day remaining to future date from now 
Csharp :: c# read single key 
Csharp :: C# Bitwise Left Shift 
Csharp :: c# azure get vm get cpu usage 
Csharp :: unity prefab button not working 
Csharp :: Unity Wait Time Fixed 
Csharp :: calculated field gridview asp.net 
Csharp :: telerik mvc grid round sum result 
Csharp :: unity sprite blurry when far 
Csharp :: esc exit winform 
Csharp :: what is the default value for an enum c# 
Csharp :: runner dotnet trace inside docker container 
Csharp :: satisfactory controller support 
Csharp :: encode < for xml 
Csharp :: C# Character and String Literals 
Csharp :: what is difference between int.Parse and toint32 in c# 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =