How to use ToCursor to iterate values on batch size?

I have 1 million records in the Mongo collection, and I will manipulate the few results after fetching them from the database. In this case, the code does not want to wait for entire records to change the value; instead, it can process the value on the batch results. Is there any way we can use the ToCursor with batch size?

Tried the below code.

 var results = database.GetCollection<TestCollection>("TestCollection")
            .Aggregate(new AggregateOptions { BatchSize = 1000 })
            .ToCursor().ForEachAsync(
            x =>
            {
                x.Name =  "dummy value";
            });

Hi @Sudhesh_Gnanasekaran and welcome to the community!!

If I understand correctly, your goal is to process the result set in a per-document basis, instead of waiting for the whole result set to arrive. Is this correct?

If yes, then typically the Collection construct in most official drivers return a cursor, with which you can iterate and process. Thus, I believe you’re on the right track with the code example you posted. However, in most drivers (e.g. Pymongo), setting batchSize do not really have a user-visible effect, since the driver will do the batching for you and return results to you in terms of documents. See python - PyMongo cursor batch_size - Stack Overflow for an example of how this is done in Pymongo.

Having said that, are there any specific reason why you want to manually set batchSize? Could you elaborate more on your use case and provide some examples?

Thanks
Aasawari

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.