Using MongoDB with C#
Mongo db is a very popular no sql database.
MS SQL |
MongoDB |
|
Data Storage Model |
Relational DBMS |
Document-oriented |
JOINs |
Yes |
No |
Transaction |
No |
|
Support agile practices |
No |
Yes |
Data schema |
Fixed |
Dynamic |
Vertical |
Horizontal |
|
Yes (depending on software edition) |
Primary-Secondary |
|
No |
Yes |
|
Query Language |
SQL query language |
JSON query language |
Secondary Indexes |
Yes |
Yes |
Triggers |
Yes |
No |
Foreign keys |
Yes |
No |
Concurrency |
Yes |
Yes |
Official Website |
||
Company |
Microsoft |
MongoDB, Inc |
Licence |
Commercial |
Open Source |
Implementation language |
C++ |
C++ |
OS support |
Windows |
Windows, Linux, OS X, Solaris |
Drivers for programming languages |
.NET, Java, PHP, Python, Ruby, Visual Basic |
Actionscript, C, C#, C++, Clojure, ColdFusion, D, Dart, Delphi, Erlang, Go, Groovy, Haskell, Java, JavaScript, Lisp, Lua, MatLab, Perl, PHP, PowerShell, Prolog, Python, R, Ruby, Scala, Smalltalk |
You can download mongodb from http://www.mongodb.org/downloads
Once you have downloaded unzip Files
Create an Folder MongoDataBase within the zip files folder
Then Go To Bin folder
Create a Bat File say StartMongo.bat
with command mongod.exe --dbpath C:\mongodb\MongoDataBase
Use can use db path as you wish.
Then Run the StartMongo.bat file it will create files in database folder.
Then You need to set classpath in ControlPanel->System->ChangeProperties
Then Go To Command Prompt run->cmd
type startmongo
So Now We are ready to use MongoDB
You can also try MongoDb free Management Tool http://robomongo.org/ which makes our life much easier.
Once You have installed it.
Create a New Connection By default MongoDb Port is 27017 please connect to it after its created
Create A Database on RighClick on New Connection
Then You can create Collection and insert Document.
Please Note that collection are like tables and document are like rows.
Here is Sample Json I have Created For My Collections Emp and Leads
CollectionName:Emp
{
"_id" : ObjectId("53e455b81b5dda17b8e4517b"),
"name" : "Pradeep",
"place" : "hyd",
"skills" : [
".net",
"sql"
],
"LeadId" : ObjectId("53e45c651b5dda17b8e4517c")
}
CollectionName:Leads
{
"_id" : ObjectId("53e45c651b5dda17b8e4517c"),
"name" : "XYZ"
}
Now We are Ready to use Mongodb with c#
Open VisualStudio i used 2012 for this sample
RightClick on your project click Manage Nuget Package
Search for Mongodb Driver
please use offical mongodb driver
Finally Please Use Code Below
The below Example shows how we can access Emp and Leads and also Emp who are referenced to particular Lead.
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson.Serialization.Attributes;
namespace MongoDBTest
{
class Program
{
static void Main(string[] args)
{
MongoClient client = new MongoClient();
var server=client.GetServer();
var db = server.GetDatabase("Media");
var Lead = db.GetCollection<Leads>("Leads").AsQueryable().First();
var collection = db.GetCollection<Emp>("Emp").AsQueryable().Where(b => b.LeadId == Lead.Id);
// var collection = db.GetCollection<Emp>("Emp").AsQueryable();
foreach (var emp in collection)
{
Console.WriteLine(emp.name);
if (emp.skills != null)
{
foreach (string s in emp.skills)
{
Console.WriteLine(s);
}
}
}
}
public class Emp
{
public ObjectId Id { set; get; }
public string name {set;get;}
public string place { set; get; }
//[BsonIgnoreIfNull]
public IList<string> skills { set; get; }
public ObjectId LeadId { set; get; }
}
public class Leads
{
public ObjectId Id { set; get; }
public string name { set; get; }
}
}
}
ThankYou,
Hope You Like It