I was wondering if there is any way to implement pagination in C# against DocumentDB with, or without, their Linq provider?
Scenario: I have an API which supports pagination, the user sends in the page they want to look at along with a pageSize, such as:
public virtual async Task<HttpResponseMessage> Get(int? page = DefaultPage, int? pageSize = DefaultPageSize)
I then use those parameters to paginate the data in the data access layer with the following code:
return query.Skip((pageNumber - 1) * pageSize).Take(pageSize);
"What is the problem then?", you might ask. Well, this approach and code works perfectly whilst using EF and SQL. The problem is that I want to start using DocumentDB but their Linq-implementation has no support for Skip. The only examples I've seen includes using the TOP keyword or continuation tokens which does not fit well with me allowing users to send in a pageNumber and pageSize.
Is there any implementation that will still allow my users to provide pageNumber
and pageSize
in the request?
Pagination is a process that is used to divide a large data into smaller discrete pages, and this process is also known as paging. Pagination is commonly used by web applications and can be seen on Google.
The paginate method counts the total number of records matched by the query before retrieving the records from the database. This is done so that the paginator knows how many pages of records there are in total.
Pagination is used in some form in almost every web application to divide returned data and display it on multiple pages within one web page. Pagination also includes the logic of preparing and displaying the links to the various pages. Pagination can be handled client-side or server-side.
Pagination is the process of splitting data into discrete pages, and you should implement it when you are building RESTful Web APIs that potentially serve huge amount of data. Imagine you have thousands or maybe millions of records in your database and your API endpoint try to return all of them at once.
SKIP is a performance issue for SQL and it's even worse for NoSQL due to their scale out design. We used MongoDB's SKIP functionality and found that it essentially reran the query from scratch throwing away all of the skipped rows. The later in the list we were skipping to, the longer the query took. So, even though it had SKIP functionality, we were forced to implement a more performant solution.
The product managers at DocumentDB understand this and are resistant to adding SKIP. If they were going to do it, I believe they would have done it when they added TOP.
For DocumentDB, the most efficient approach is to use the continuation token and cache all of the results in order up to (and even predictably beyond) where your user wants. Continuation tokens survive for a long time, so you don't need to fetch all pages immediately.
Whilst this doesn't answer your question specifically, for future Googlers, Document DB supports paging via continuation tokens. I've written it up in detail here. The code you need it this:
var endpoint = "document db url";
var primaryKey = "document db key";
var client = new DocumentClient(new Uri(endpoint), primaryKey);
var collection = UriFactory.CreateDocumentCollectionUri("database id", "collection id");
var options = new FeedOptions
{
MaxItemCount = 100 // <- Page size
};
var query = client.CreateDocumentQuery<Document>(collection, options).AsDocumentQuery();
while (query.HasMoreResults)
{
var result = await query.ExecuteNextAsync<Document>();
// Process paged results
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With