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
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:
Finally, your MongoDB server will run in your system. See the following.
mongo.exe server
mongod.exe server
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.
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".
- Select "Browse" then click on "Browse".
- After clicking on "Browser" then select DLL files then click on "Add" as in the following figure.
- Select some files and then click "OK".
Connect to MongoDB with C# and then save BSON document in MongoDB database
The following is the Console Application code in the C# language .
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml.Linq;
- using MongoDB.Bson;
- using MongoDB.Driver;
-
- namespace ConsoleApplication1 {
- classProgram {
- staticvoid Main(string[] args) {
-
- MongoServer mongo = MongoServer.Create();
- mongo.Connect();
-
- Console.WriteLine("Connect to MongoDB Server");
- Console.WriteLine();
-
- var db = mongo.GetDatabase("My_Database");
-
- using(mongo.RequestStart(db))
- {
- var collection = db.GetCollection < BsonDocument > ("My_Collection");
-
- BsonDocument book = newBsonDocument()
- .Add("_id", BsonValue.Create(BsonType.ObjectId))
- .Add("author", "pradhan")
- .Add(" title", "My exprience");
-
- collection.Insert(book);
- var query = newQueryDocument("author", "pradhan");
- foreach(BsonDocument item in collection.Find(query))
- {
- Console.WriteLine("Key Value ");
- Console.WriteLine();
- foreach(BsonElement element in item.Elements)
- {
- Console.WriteLine("{0} {1}", element.Name, element.Value);
- }
- }
-
- Console.WriteLine();
- }
-
- Console.WriteLine();
- Console.Read();
-
- mongo.Disconnect();
- }
- }
- }
Output
When you run that code you will see the following output.
Note: if you want to see data in the MongoDB Server, then use the following procedure.