
MongoDB, As I said in my title it’s just fun to deal with. Believe me you also will say the same at the end of this post. So let’s go into NO SQL World.
What is No SQL ?
No more sql here pls. This is a world without SQL. So how to perform operations with db ?. Follow with me……
What is Mongo DB ?
Mongo DB is a NO SQL Database server. Most of the popular programming languages got drivers to connect and play with Mongo. No SQL is a new world of storage. You don’t find any tables or rows to store your data. So How ? There is a different representation to store data. We don’t have tables anymore but we have something called collections. We don’t have Rows or Records anymore bu we have something called documents. Basically if you want to store a bunch of contact details, you first create a ‘Contacts’ collection and store each contact detail as a ‘Contact’ document.
Let’s get started!!!
Downloading Mongo DB
1 ) Take a quick look in to Quick Start Guide to install the DB.
2) It’s good to install as a Windows service. Execute the following command to install as a windows service.
D:\mongodb\bin>mongod –dbpath D:\mongodb –logpath D:\mongodb\log.txt –install
3) Start the service.
4) Download C# Driver from the following location.
http://github.com/samus/mongodb-csharp/tree/master
There are several drivers available for C#. I’m going to use this one in this tutorial.
Connection to DB with C# Driver
Create a new C# project (Console Project) and add reference to C# Driver you already downloaded.
Mongo mongo = new Mongo(ConnectionString);
mongo.Connect();
IMongoDatabase db = mongo.GetDatabase("Test");
That’s it. We Connected to DB called ‘Test’.
Let’s say which collection we are going to use.
IMongoCollection Contacts = db.GetCollection("Contacts");
Well, we also said we are going to use Collection ‘Contacts’.
Now we need to insert a new Record. Oh Sorry ! No more records. We need to insert a ‘Document’.
Document doc = new Document();
doc["Name"] = "John";
Contacts.Insert(doc);
mongo.Disconnect();
So now we inserted a document with a ‘Name’ property. The cool things about NO SQL DB and Documents is it’s schema free. Now we can insert another document to the same collection with another property called ‘id’ or something.
Full Source code of the above example would look like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MongoDB
{
class Program
{
static void Main(string[] args)
{
Mongo mongo = new Mongo();
mongo.Connect();
IMongoDatabase db = mongo.GetDatabase("Test");
IMongoCollection Contacts = db.GetCollection("Contacts");
Document doc = new Document();
doc["Name"] = "John";
Contacts.Insert(doc);
mongo.Disconnect();
Console.WriteLine("Inserted");
Console.ReadLine();
}
}
}
Using Connection String
You also can use connection string to connect to a specific Mongo DB server.
string ConnectionString = "YourConnectionStringHere";
Mongo mongo = new Mongo(ConnectionString);
Mongo DB Hosting
There are several mongoDB hosting services available. https://mongohq.com/home is a good service which has a free star up package to test MongoDB Application. You can use connection string to connection to these Hosted MongoDBs.
Recent Comments