2

I have implemented a class to read from Cosmos DB using the MongoDB.Driver.IMongoClient.

I need pagination as there are a large number of items in each collection, I thought I had achieved this with the following:

private async Task<ICollection<SomeClass>> ReadMessages(IMongoCollection<SomeClass> collection, FilterDefinition<SomeClass> filter, int page, int pageSize)
{
    var found = new List<SomeClass>();

    using (var cursor = await collection
        .Find(filter)
        .Skip((page - 1) * pageSize)
        .Limit(pageSize)
        .ToCursorAsync())
    {
        while (await cursor.MoveNextAsync())
        {
            var batch = cursor.Current;

            found.AddRange(batch.Select(x => x);
        }
    }

    return found.ToArray();
}

We're now running into "Request rate is large" exceptions for higher page numbers.

From what I can see from here and here Cosmos DB doesn't support Skip so would anybody be able to explain what IMongoClient is doing here?

Also, what is the correct way to achieve this using a continuation token?

RagtimeWilly
  • 5,265
  • 3
  • 25
  • 41
  • I suspect the first question is answered [here](https://stackoverflow.com/a/35120109/3181933), and given it has to go through the Mongo driver, I'm wondering if perhaps there isn't a solution here. – ProgrammingLlama Aug 29 '18 at 07:28
  • Since `skip` is not a native function for Cosmos DB's SQL API, the MongoDB API will likely have the same limitations (e.g. MongoDB's `skip` possibly being implemented via paging in the background). What is your collection's current RU scale? If you're returning a significant amount of documents, you might have to increase RU. – David Makogon Aug 29 '18 at 10:44
  • Also note: The MongoDB client knows nothing of specific implementation, since it's a standard MongoDB client. It knows nothing of Cosmos DB; it just talks with the MongoDB API endpoint, as it would with any other MongoDB database. – David Makogon Aug 29 '18 at 10:46

0 Answers0