Retrieve the data from the MongoDB using C# and .NET

Hi,
   After inserting some data into MongoDB collection , here are three ways to retrieve the data from the Mongo DB using C#, Don’t forget to add MongoDB reference to the solution before performing the retrieve operation.

C# code:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Core;

namespace RetrivData
{
    class Program
    {
        static void  Main(string[] args)
        {
            CallMain(args).Wait();
            Console.ReadLine();
        }
        static async Task CallMain(string[] args)
        {
            var conString = "mongodb://localhost:27017";
            var Client = new MongoClient(conString);
            var DB = Client.GetDatabase("test");
            var collection = DB.GetCollection<BsonDocument>("store");
          
    //Method 1
    using (var cursor = await collection.Find(new BsonDocument()).ToCursorAsync())
    {
        while (await cursor.MoveNextAsync())
        {
            foreach (var doc in cursor.Current)
            {
                Console.WriteLine(doc);
            }
        }
    }

    // Method 2
    var list = await collection.Find(new BsonDocument()).ToListAsync();
    foreach (var dox in list)
    {
        Console.WriteLine(dox);
    }

 // Method 3
 await collection.Find(new BsonDocument()).ForEachAsync(X=>Console.WriteLine(X));
    
        }
    }
}