Unofficial IAsyncEnumerable support for MongoDB C# Driver.
$ dotnet add package MongoDB.Driver.Linq.AsyncEnumerableUnofficial IAsyncEnumerable support for MongoDB C# Driver.
Provides ToAsyncEnumerable() extensions for MongoDB IAsyncCursor and IAsyncCursorSource.
Asynchronous enumeration is implemeted so that the iterator fetches documents from database to memory one batch at time.
The size of the batch can be controller by BatchSize property of e.g. AggregateOptions or FindOptions.
MongoDB.Driver.Linq namespace by using MongoDB.Driver.LinqToAsyncEnumerable() extension for either IAsyncCursor or IAsyncCursorSourceToAsyncEnumerable()Find()using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
var client = new MongoClient();
var database = client.GetDatabase("ExampleDatabase");
var collection = database.GetCollection<BsonDocument>("ExampleCollection");
await foreach (var document in collection.Find(_ => true).ToAsyncEnumerable())
{
Console.WriteLine(document.ToString());
}
Aggregate() by fetching documents in batches of 1000 documentsusing MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
var client = new MongoClient();
var database = client.GetDatabase("ExampleDatabase");
var collection = database.GetCollection<BsonDocument>("ExampleCollection");
var options = new AggregateOptions { BatchSize = 1000 };
await foreach (var document in collection.Aggregate(options).ToAsyncEnumerable())
{
Console.WriteLine(document.ToString());
}