Simple CRUD With MongoDB

The following is the procedure for doing CRUD operations in MongoDB.

Step 1

Create a folder such as D:\data\db.

Step 2

Download MongoDB binaries from: http://www.mongodb.org/downloads and unzip it to the “D” drive.

Step 3

Double-click on “mongod” inside “.\mongodb-win32-i386-2.6.4\bin”. That will open a new window. Keep this window opened. This will open a port Mongo with a port (in my case it is 27017).

mongod

Open http://localhost:27017/.

Step 4

Create a Window application with Visual Studio.

local host

Step 5

Install the official MongoDB C# driver. Run “PM> Install-Package mongocsharpdriver” in the Package Manager Console .

testmongo

Step 6

To create a database and record:

  1. MongoServer mongo = MongoServer.Create();  
  2.             mongo.Connect();  
  3.             var db = mongo.GetDatabase("Organization");  
  4.   
  5.             using (mongo.RequestStart(db))  
  6.             {  
  7.                 var collection = db.GetCollection<BsonDocument>("Employees");  
  8.   
  9.                 BsonDocument Employee = new BsonDocument()  
  10.                         .Add("_id", System.Guid.NewGuid().ToString())  
  11.                         .Add("Name", txtName.Text)  
  12.                         .Add("Eamil", txtEmail.Text)  
  13.                         .Add("Phone", txtPhone.Text);  
  14.   
  15.                 collection.Insert(Employee);  
  16.   
  17.   
  18.                 textBox1.Text = textBox1.Text + System.Environment.NewLine + "Inserted....";  
  19.                 mongo.Disconnect();  
  20.                 ReadAll();  
  21.             }  
Step 7

To search a record:
  1. MongoServer mongo = MongoServer.Create();  
  2.            mongo.Connect();   
  3.            textBox1.Text = textBox1.Text + System.Environment.NewLine + "Searching....";  
  4.            var db = mongo.GetDatabase("Organization");  
  5.            using (mongo.RequestStart(db))  
  6.            {  
  7.                 
  8.                var collection = db.GetCollection<BsonDocument>("Employees");  
  9.   
  10.                var query = new QueryDocument("Name", txtNameSearch.Text);  
  11.   
  12.                     foreach (BsonDocument item in collection.Find(query))   
  13.                     {  
  14.                         BsonElement Name = item.GetElement("Name");  
  15.                         BsonElement Eamil = item.GetElement("Eamil");  
  16.                         BsonElement Phone = item.GetElement("Phone");  
  17.                         BsonElement id = item.GetElement("_id");  
  18.   
  19.                         txtNameSearch.Text = Name.Value.ToString();  
  20.                         txtEamilSearch.Text = Eamil.Value.ToString();  
  21.                         txtPhoneSearch.Text = Phone.Value.ToString();  
  22.                         txtID.Text = id.Value.ToString();  
  23.                     }   
  24.            }  
  25.            mongo.Disconnect();Step 7: to update a record   
  26.     MongoServer mongo = MongoServer.Create();  
  27.            mongo.Connect();  
  28.            var db = mongo.GetDatabase("Organization");  
  29.   
  30.            using (mongo.RequestStart(db))  
  31.            {  
  32.                var collection = db.GetCollection<BsonDocument>("Employees");  
  33.   
  34.                BsonDocument Employee = new BsonDocument()  
  35.                        .Add("_id", txtID.Text)  
  36.                        .Add("Name", txtNameSearch.Text)  
  37.                        .Add("Eamil", txtEamilSearch.Text)  
  38.                        .Add("Phone", txtPhoneSearch.Text);  
  39.   
  40.                collection.Save(Employee);  
  41.                textBox1.Text = textBox1.Text + System.Environment.NewLine + "Update....";  
  42.                mongo.Disconnect();  
  43.            }  
Step 8

To read all records:
  1. MongoServer mongo = MongoServer.Create();  
  2.             mongo.Connect();  
  3.             textBox1.Text = textBox1.Text + System.Environment.NewLine + "Reading All....";  
  4.             var db = mongo.GetDatabase("Organization");  
  5.             using (mongo.RequestStart(db))  
  6.             {  
  7.                 var collection = db.GetCollection<BsonDocument>("Employees");            
  8.                 foreach (BsonDocument item in collection.FindAll())  
  9.                 {  
  10.                     BsonElement Name = item.GetElement("Name");  
  11.                     BsonElement Eamil = item.GetElement("Eamil");   
  12.                     textBox1.Text=  textBox1.Text+System.Environment.NewLine+string.Format("Name: {0}, email: {1}", Name.Value.ToString(), Eamil.Value.ToString());   
  13.                       
  14.                 }  
  15.             }  
  16.             mongo.Disconnect();  
Step 9

To delete a record:
  1. MongoServer mongo = MongoServer.Create();  
  2.        mongo.Connect();  
  3.        var db = mongo.GetDatabase("Organization");  
  4.   
  5.        using (mongo.RequestStart(db))  
  6.        {  
  7.            var collection = db.GetCollection<BsonDocument>("Employees");  
  8.   
  9.            collection.Remove(new QueryDocument("_id", txtID.Text));  
  10.              
  11.            textBox1.Text = textBox1.Text + System.Environment.NewLine + "deleted....";  
  12.            mongo.Disconnect();  
  13.        }  
Delete a record

 


Similar Articles