Save BSON Document In MongoDB Using C# Driver

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.
  • Go to Solution Explorer then right-click on the application name, then click "Add Reference".
     
    f2.jpg
     
  • Select "Browse" then click on "Browse".
     
    f3.jpg
     
  • After clicking on "Browser" then select DLL files then click on "Add" as in the following figure.
     
    f4.jpg
     
  • Select some files and then click "OK".
     
    f6.jpg
     
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.   
  10. namespace ConsoleApplication1 {  
  11.     classProgram {  
  12.         staticvoid Main(string[] args) {  
  13.   
  14.             MongoServer mongo = MongoServer.Create(); //Create object of MongoDB server  
  15.             mongo.Connect(); // call to connect MongoDb server  
  16.   
  17.             Console.WriteLine("Connect to MongoDB Server");  
  18.             Console.WriteLine();  
  19.   
  20.             var db = mongo.GetDatabase("My_Database"); // Call to "My_ Collection" database which create in MongoDB  
  21.   
  22.             using(mongo.RequestStart(db)) // call to request send to MongoDB  
  23.             {  
  24.                 var collection = db.GetCollection < BsonDocument > ("My_Collection");  
  25.                 /* call to MongoDB which create collection in "My_Database"*/  
  26.                 BsonDocument book = newBsonDocument() // Create BSON document which document name is "Book"  
  27.                     .Add("_id", BsonValue.Create(BsonType.ObjectId)) // Create "Object _id"   
  28.                     .Add("author""pradhan"// Add data in "book" document (key and value format)  
  29.                     .Add(" title""My exprience");  
  30.   
  31.                 collection.Insert(book); /* Insert BSON document type data (name is "book") in collection                                                                                                                                    ("My_collection") */  
  32.                 var query = newQueryDocument("author""pradhan"); /*Create Query for view data, which save in collection                                                                                                                                                      ("My_Collection"), in MongoDB database ("My_Databse") */  
  33.                 foreach(BsonDocument item in collection.Find(query)) // create foreach loop for select item in collection  
  34.                 {  
  35.                     Console.WriteLine("Key           Value ");  
  36.                     Console.WriteLine();  
  37.                     foreach(BsonElement element in item.Elements) // create foreach loop for select element in  item  
  38.                     {  
  39.                         Console.WriteLine("{0}            {1}", element.Name, element.Value); // print element name and value  
  40.                     }  
  41.                 }  
  42.   
  43.                 Console.WriteLine();  
  44.             }  
  45.   
  46.             Console.WriteLine();  
  47.             Console.Read();  
  48.   
  49.             mongo.Disconnect(); // call to disconnect MongoDB server  
  50.         }  
  51.     }  

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
 


Similar Articles