Search code examples
c#mongodbmongodb.driver

C# mongodb driver 2.2.3 how to set batchSize for cursor


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.


Solution

  • 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
        });
    }