Introduction
In this article we will learn how to do the CRUD operations with MongoDB using ASP.NET. MongoDB is a document database and SQL Server is a relational database, often used many times with ASP.NET projects. But this time we will implement the MongoDB with ASP.NET. The advantage over the SQL-Server is scalability, agility, however these are not the only redeeming qualities that database MongoDB possess.
As we know that MongoDB is a Document Database. This is the new breed of the database, that is designed for storing, retrieving and managing document oriented information. The main objective of this database is to store data in a standard format or encoding.
Some features of MongoDB are :
- Continues Data Availability
- Real Location Independence
- Flexible Data Models
- Full Index Support
- Replication and High Availability
- Auto-Sharding
Prerequisites
- Visual Studio
- MongoDB
- MongoDB Driver for CSharp
Installation process of MongoDB Driver for C#
The MongoDB Driver was officially developed by the MongoDB Team. We need to download the driver from github or the official website of the MongoDB. There is another way to install driver from Nuget Package Manager from Visual Studio. In this article we will see the installation process through Nuget Package Manager. In this driver are two major classes.
- BSON Library
- C# Driver Library
Step 1
Open the Visual Studio .
Step 2
Choose empty project.
Step 3
Open The Nuget Package Manager.
This is the simplest way to install the MongoDB Driver from the Visual Studio. After installation we will get MongoDB.Bson and MongoDB.Driver in the reference folder.
Step 4
Be confirmed after installation.
Step 5
Start the MongoDB Server.
To start the server we need to type "mongod - - dbpath db" in the command prompt.
Step 6
Make a connection in the web config file as in the following:
- <connectionStrings>
- <add name="con" connectionString="Server=127.0.0.1:27017"/>
- </connectionStrings>
Step 7
Create and select.
- public DAL()
- {
- con = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
- server = MongoServer.Create(con);
- emptbl = server.GetDatabase("Employee");
-
- }
- public List<Employee> getEmployeeList()
- {
- List<Employee> list = new List<Employee>();
- var collection = emptbl.GetCollection<Employee>("Employee");
- foreach (Employee emp in collection.FindAll())
- {
- list.Add(emp);
- }
- return list;
-
- }
Step 8
Insert.
- public void insert(Employee emp)
- {
- try
- {
- MongoCollection<Employee> collection = emptbl.GetCollection<Employee>("Employee");
- BsonDocument employee = new BsonDocument
- {
- {"empName",emp.empName},
- {"empId",emp.empId},
- {"salary",emp.salary},
- {"address", emp.address},
- {"phone",emp.phone}
- };
- collection.Insert(employee);
- }
- catch { };
- }
Step 9
Update.
- public void updateEmployee(Employee emp)
- {
- MongoCollection<Employee> collection = emptbl.GetCollection<Employee>("Employee");
- IMongoQuery query = Query.EQ("_id", emp._id);
- IMongoUpdate update = MongoDB.Driver.Builders.Update.Set("empName", emp.empName)
- .Set("empId", emp.empId)
- .Set("salary", emp.salary)
- .Set("address", emp.address)
- .Set("phone", emp.phone);
- collection.Update(query, update);
-
- }
Step 10
Delete
- public void DeleteEmployee(ObjectId id)
- {
- MongoCollection<Employee> collection = emptbl.GetCollection<Employee>("Employee");
- IMongoQuery query = Query.EQ("_id", id);
- collection.Remove(query);
- }
In all these operations, we will now sum up all these things and bind into a GridView in ASP.NET. A GridView have all four operations ( insert, update,select and delete) , so it will be an easy. And before we take the next step we have completed all the important tasks. Install the mongoDB, install the mongoDB driver, create a DAL (Data Access Layer). Now we will use the entire code and implement it into an ASP.NET page.
HTML Page
Code
Output: Displayed all records in GridView.
Output: When we hit the Add button, this pop up box will display for inserting a new record.
Output: When we hit the edit button , we can edit any record.
Summary
In this article we learned the Mongo C# driver and how the driver interacts with the MongoDB server. We also learned the basic CRUD operations using the Mongo Driver.