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).
Open http://localhost:27017/.
Step 4
Create a Window application with Visual Studio.
Step 5
Install the official MongoDB C# driver. Run “PM> Install-Package mongocsharpdriver” in the Package Manager Console .
Step 6
To create a database and record:
- MongoServer mongo = MongoServer.Create();
- mongo.Connect();
- var db = mongo.GetDatabase("Organization");
-
- using (mongo.RequestStart(db))
- {
- var collection = db.GetCollection<BsonDocument>("Employees");
-
- BsonDocument Employee = new BsonDocument()
- .Add("_id", System.Guid.NewGuid().ToString())
- .Add("Name", txtName.Text)
- .Add("Eamil", txtEmail.Text)
- .Add("Phone", txtPhone.Text);
-
- collection.Insert(Employee);
-
-
- textBox1.Text = textBox1.Text + System.Environment.NewLine + "Inserted....";
- mongo.Disconnect();
- ReadAll();
- }
Step 7
To search a record:
- MongoServer mongo = MongoServer.Create();
- mongo.Connect();
- textBox1.Text = textBox1.Text + System.Environment.NewLine + "Searching....";
- var db = mongo.GetDatabase("Organization");
- using (mongo.RequestStart(db))
- {
-
- var collection = db.GetCollection<BsonDocument>("Employees");
-
- var query = new QueryDocument("Name", txtNameSearch.Text);
-
- foreach (BsonDocument item in collection.Find(query))
- {
- BsonElement Name = item.GetElement("Name");
- BsonElement Eamil = item.GetElement("Eamil");
- BsonElement Phone = item.GetElement("Phone");
- BsonElement id = item.GetElement("_id");
-
- txtNameSearch.Text = Name.Value.ToString();
- txtEamilSearch.Text = Eamil.Value.ToString();
- txtPhoneSearch.Text = Phone.Value.ToString();
- txtID.Text = id.Value.ToString();
- }
- }
- mongo.Disconnect();Step 7: to update a record
- MongoServer mongo = MongoServer.Create();
- mongo.Connect();
- var db = mongo.GetDatabase("Organization");
-
- using (mongo.RequestStart(db))
- {
- var collection = db.GetCollection<BsonDocument>("Employees");
-
- BsonDocument Employee = new BsonDocument()
- .Add("_id", txtID.Text)
- .Add("Name", txtNameSearch.Text)
- .Add("Eamil", txtEamilSearch.Text)
- .Add("Phone", txtPhoneSearch.Text);
-
- collection.Save(Employee);
- textBox1.Text = textBox1.Text + System.Environment.NewLine + "Update....";
- mongo.Disconnect();
- }
Step 8
To read all records:
- MongoServer mongo = MongoServer.Create();
- mongo.Connect();
- textBox1.Text = textBox1.Text + System.Environment.NewLine + "Reading All....";
- var db = mongo.GetDatabase("Organization");
- using (mongo.RequestStart(db))
- {
- var collection = db.GetCollection<BsonDocument>("Employees");
- foreach (BsonDocument item in collection.FindAll())
- {
- BsonElement Name = item.GetElement("Name");
- BsonElement Eamil = item.GetElement("Eamil");
- textBox1.Text= textBox1.Text+System.Environment.NewLine+string.Format("Name: {0}, email: {1}", Name.Value.ToString(), Eamil.Value.ToString());
-
- }
- }
- mongo.Disconnect();
Step 9
To delete a record:
- MongoServer mongo = MongoServer.Create();
- mongo.Connect();
- var db = mongo.GetDatabase("Organization");
-
- using (mongo.RequestStart(db))
- {
- var collection = db.GetCollection<BsonDocument>("Employees");
-
- collection.Remove(new QueryDocument("_id", txtID.Text));
-
- textBox1.Text = textBox1.Text + System.Environment.NewLine + "deleted....";
- mongo.Disconnect();
- }