Skip to main content

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));
    
        }
    }
}

Comments

  1. How can we make user search for a specific data (say, search by ID), by Making the user type the specific id in textbox and hit the search button using asp .net c#

    ReplyDelete
    Replies
    1. I tried the following implementation and it seemed to work:
      Here, my collection "store" contains the fields : "email", and "name"
      There are only 2 documents in my collection and hence the output contains output from 2 documents.

      using (var cursor = await collection.Find(new BsonDocument()).ToCursorAsync())
      {
      while (await cursor.MoveNextAsync())
      {
      foreach (var doc in cursor.Current)
      {

      Console.WriteLine(doc["email"]);
      Console.WriteLine(doc["name"]);
      }
      }
      }


      OUTPUT:

      abcd@gmail.com
      Abcd
      lmno@gmail.com
      Lmno

      .........................................................................
      One issue i faced is that, this was possible when I used:
      Console.WriteLine(doc["email"]);

      But only: doc["email"]; didn't work. I'm unable to use the extracted data other than to print in my console.
      .........................................................................

      Hope this was useful!

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Just tried the following piece of code. It seems to work with a "typecast".
      for int, we would need to typecast as (int)doc["field_Name"];
      similarly for float, double, string etc.

      while (await cursor.MoveNextAsync())
      {
      foreach (var doc in cursor.Current)
      {
      string em = (string)doc["email"];
      Console.WriteLine(em);
      }
      }


      This Works!! Hope this helps!!

      Delete

Post a Comment