Search code examples
c#mongodbmongodb-.net-driver

Execute raw query that could handle all numerous of documents mongo driver c#


I have the situation that filtering condition on collection is quite long. And I prefer to write it by hand and using the mongoDb RunCommandAsync method in C# driver to get the Bson Document as result then Deserialize it back to object.

As the doc said it will return a BsonDocument, instead of a Cursor to iterate with.

I made a small test from a collection with some dummy filter condition as below, and i got ~215k docs from robo 3T, but when it comes to RunCommandAsync in c#, it default return 101 document. I attempted to increase the batchSize to 200.000, but the execution can only return ~22k docs(which i guess it's the limitation of 4MB per batch operation).

var cmdDoc = @"{
'find': 'NotificationHistories',
'filter': {'CreatedTime': {$exists: true}}
'batchSize': 200000,
'singleBatch': true
}";
var cmd = new JsonCommand<BsonDocument>(cmdDoc);
var res = await database.RunCommandAsync(cmd, ReadPreference.PrimaryPreferred, CancellationToken.None);

So my final question is:

Is there anyway we can execute raw query that could handle all the numerous of documents ? The result can be any of Cursor, BsonDocument, Array of BsonDocument,...

Or there's just no possible way to workaround this but use the normal Find or Aggregate method of Collection ?

Any clue would be appriciate, since i felt its quite long and cumbersome writing those especially with Unwind, Group, Lookup,...


Solution

  • 101 records is a number of records in the firstBatch of your cursor, iterating of next records(batch by batch) should be done via getMore command (but I think it's a wrong that you think in this way, I see no reason to avoid a regular way).