IAsyncCursorHow to Iterate and Read

Iterate and Read using IAsyncCursor

In this article, we will look into a few techniques of iterating over the IAsyncCursor collection. As we know, the MongoDB driver exposes multiple database-related extension methods, that let you perform basic CRUD operations on the given database. These basic extension methods often use IAsyncCursor which help in enumerating elements in collection for any given filter criteria.

Technically IAsyncCursor is equivalent to IEnumerable which provides the capability of iterating over the collection in both synchronous and asynchronous ways.

There are two methods exposed by the IAsyncursor interface exposes below members,

  • MoveNext
  • MoveNextAsync

As I mentioned above this interface can be used for Sync and Async operations.

Read the context from IAsyncCursor

IAsyncCursor exposes below a few methods which help you enumerate and read the result,

1. ForEachAsync

2. ToList()

3. ToListAsync

4. ToEnumerator

Example Method for IAsyncCursor

Below is an example method that returns IAsyncCursor

 
public async Task<IAsyncCursor<TEntity>> GetAsync()
        {  
            return await _dbCollection.FindAsync(Builders<TEntity>.Filter.Empty);
        }

We shall now see below a few approaches to iterate over collections.

IAsyncCursor – Using ForEachAsync()

Below is an example for ForEachAsync()

   
   var output = await bookRepo.GetAsync();

   await output.ForEachAsync(x => Console.WriteLine("Name:" + x.Name, 
                                  "Author:" + x.Author,
                                  "Price:" + x.Price));

IAsyncCursor – Using ToList()

Below is another example using ToList()

            var output = await bookRepo.GetAsync();

            var result = output.ToList();

            foreach (var book in result)
            {
                Console.WriteLine("Name:"+ _book.Name);
                Console.WriteLine("Author:" + _book.Author);
                Console.WriteLine("Price:" + _book.Price); 
            }

IAsyncCursor – Using IEnumerator

Below is an example of IAsyncCursor – Using IEnumerator.

             
             var output = await bookRepo.GetAsync();

             var result = output.ToEnumerable();

            foreach (var book in result)
            {
                Console.WriteLine("Name:"+ _book.Name);
                Console.WriteLine("Author:" + _book.Author);
                Console.WriteLine("Price:" + _book.Price); 
            }

Other references :

If interested to learn how to mock IAsyncCursor, please refer to below article.

That’s All !!

Do you have any other better suggestions for dealing with Asyncursor? Please sound off your comments below.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *