15

I am using official C# driver for MongoDB 2.2.3

How can I set batch size for the cursor using the C# driver?

With javascript I can create a cursor and set batch size for it:

var cursor = db.statistics.find(query).batchSize(100)

and I can iterate through all items using the following statement:

while(cursor.objsLeftInBatch()>0){
    var doc = cursor.next();
    //process doc
}

I would like to have the same behaviour in C# with async/await support. I know that I can use cursor from C# but it's default batch size is 4MB. This is too match to return to the client with one call.

denisvlah
  • 434
  • 1
  • 4
  • 13
  • MongoDB legacy API does provide method to control batch size but I'm not sure about modern API. see http://api.mongodb.org/csharp/current/html/P_MongoDB_Driver_MongoCursor_BatchSize.htm – Saleem Apr 03 '16 at 23:04

1 Answers1

29

You can set the batch size in the FindOptions parameter of FindAsync.

Here's the basic pattern to explicitly handle the batches:

var filter = new BsonDocument();
var options = new FindOptions<BsonDocument>
{
    // Get 100 docs at a time
    BatchSize = 100
};

using (var cursor = await test.FindAsync(filter, options))
{
    // Move to the next batch of docs
    while (await cursor.MoveNextAsync())
    {
        var batch = cursor.Current;
        foreach (var doc in batch)
        {
            // process doc
        }
    }
}

But you can also call ForEachAsync on the cursor and the batches will be transparently fetched on demand:

using (var cursor = await test.FindAsync(filter, options))
{
    await cursor.ForEachAsync(doc =>
    {
        // process doc
    });
}
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • Thank you a lot JonyHK, I have checked it with profile, everything works fine. – denisvlah Apr 04 '16 at 09:37
  • @johnnyHK - how does the batching work in your `ForEachAsync` scenario? For example, if you have a batchSize of `100` and after those 100 records have been 'processed', you then want to log something / Console.WriteLine / `BulkWriteAsync` or something ? – Pure.Krome Oct 02 '21 at 07:37