none
What is the best way to do "paging" on a query? RRS feed

  • Question

  • Hi,

    I know that paging isn't really supported yet but I want to know if there's a work around to do it. I want to query player's rank based on the their score and only query for a certain amount of documents (such as Top 10, or only display 5 players who are higher/lower) rather than getting the whole collections

    Is there a way to do this?

    Thanks

    Monday, November 23, 2015 5:10 AM

Answers

  • Hi,

    Are you using one of the SDKs? Taking the .Net one for instance, you could do something like:

    //Helper functions
    private static async Task<Database> GetDatabaseAsync(string id)
    {
    return client.CreateDatabaseQuery().Where(c => c.Id == id).ToArray().FirstOrDefault();
    }
    
    private static async Task<DocumentCollection> GetCollectionAsync(Database db, string id)
    {
    	return = client.CreateDocumentCollectionQuery(db.SelfLink).Where(c => c.Id == id).ToArray().FirstOrDefault();
    }
    
    //Main code
    using (client = new DocumentClient(new Uri("https://serviceurl"), "auth key")) {
    Database database = await GetDatabaseAsync("mydb");
    
    
    DocumentCollection collection = await GetCreateCollectionAsync(database, "mycollection");
    int MaxItemsPerPage  = 10;
    
    var query = client.CreateDocumentQuery<MyObjectType>(collection.SelfLink, new FeedOptions() { MaxItemCount = MaxItemsPerPage }).AsDocumentQuery();
    var feedResponse = await query.ExecuteNextAsync<MyObjectType>();
    //This is the token that lets you paginate
    string continuation = feedResponse.ResponseContinuation;
    
    var items = feedResponse.AsEnumerable().ToList();
    
    //next page
    query = client.CreateDocumentQuery<MyObjectType>(collectionLink, new FeedOptions { MaxItemCount = MaxItemsPerPage , RequestContinuation = continuation }).AsDocumentQuery();
    feedResponse = await query.ExecuteNextAsync<MyObjectType>();
    
    var moreitems = feedResponse.AsEnumerable().ToList();

    The important part is to use the Continuation token and to pass it to subsecuent queries.

    Wednesday, December 2, 2015 11:17 AM