Introduction

MongoDB uses BSON as the data storage and network transfer format for "documents". BSON is a binary-encoded serialization of JSON-like documents. BSON is designed to be lightweight, traversable, and efficient. MongoDB is representative of NoSQL databases. It is a document-oriented database, which means that data will be stored in documents in BSON format.

Setting up MongoDB in your system

First, you have to download the MongoDB Zip file from the following site: http://downloads.mongodb.org/win32/mongodb-win32-x86_64-2.4.5.zip and then install MongoDB in your system. If you don't know about installing then use the following link:http://www.dbtalks.com/UploadFile/e6aced/how-to-install-mongodb-in-your-system/.
Install C# driver in your system
The C# driver consists of two libraries: the BSON Library and the C# driver. The BSON library can be used independently of the C# Driver if desired. The C# Driver requires the BSON library. First, you have to download the C# driver at the link: http://driver-downloads.mongodb.org/dotnet/index.html. Then download the latest version of the C# driver in msi format. Then install the C# driver into your system. After installing the C# driver, store it in your system in this path: C:\Program Files (x86)\MongoDB\CSharpDriver 1.8.2. You have seen two DLL files, the first is MongoDB.Bson.dll and the second is MongoDB.Driver.dll.These files are used in connecting to MongoDB with C#.Net.
Run MongoDB Server
Go to the "C:\mongodb\bin" address in your system and then run mongo.exe and mongod.exe as in the following figure:
f7.jpg
Finally, your MongoDB server will run in your system. See the following.
mongo.exe server
f8.jpg
mongod.exe server
f9.jpg
Open Microsoft Visual Studio
I have learned about Visual Studio 2012. But you need it to work with any Visual Studio version. First, go to "File" -> "New" -> "Project..." and then create a new console application and provide any name.
f1.jpg
Add reference file
After creating a console application you need to add two references from the C# driver. See that in the following.
Connect to MongoDB with C# and then save BSON document in MongoDB database
The following is the Console Application code in the C# language .

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml.Linq;
  7. using MongoDB.Bson; // Add BSON Library file
  8. using MongoDB.Driver; // Add Driver Library file
  9. namespace ConsoleApplication1 {
  10. classProgram {
  11. staticvoid Main(string[] args) {
  12. MongoServer mongo = MongoServer.Create(); //Create object of MongoDB server
  13. mongo.Connect(); // call to connect MongoDb server
  14. Console.WriteLine("Connect to MongoDB Server");
  15. Console.WriteLine();
  16. var db = mongo.GetDatabase("My_Database"); // Call to "My_ Collection" database which create in MongoDB
  17. using(mongo.RequestStart(db)) // call to request send to MongoDB
  18. {
  19. var collection = db.GetCollection < BsonDocument > ("My_Collection");
  20. /* call to MongoDB which create collection in "My_Database"*/
  21. BsonDocument book = newBsonDocument() // Create BSON document which document name is "Book"
  22. .Add("_id", BsonValue.Create(BsonType.ObjectId)) // Create "Object _id"
  23. .Add("author", "pradhan") // Add data in "book" document (key and value format)
  24. .Add(" title", "My exprience");
  25. collection.Insert(book); /* Insert BSON document type data (name is "book") in collection ("My_collection") */
  26. var query = newQueryDocument("author", "pradhan"); /*Create Query for view data, which save in collection ("My_Collection"), in MongoDB database ("My_Databse") */
  27. foreach(BsonDocument item in collection.Find(query)) // create foreach loop for select item in collection
  28. {
  29. Console.WriteLine("Key Value ");
  30. Console.WriteLine();
  31. foreach(BsonElement element in item.Elements) // create foreach loop for select element in item
  32. {
  33. Console.WriteLine("{0} {1}", element.Name, element.Value); // print element name and value
  34. }
  35. }
  36. Console.WriteLine();
  37. }
  38. Console.WriteLine();
  39. Console.Read();
  40. mongo.Disconnect(); // call to disconnect MongoDB server
  41. }
  42. }
  43. }
Output
When you run that code you will see the following output.
f10.jpg
Note: if you want to see data in the MongoDB Server, then use the following procedure.
f11.jpg