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?