CRUD Operation Using ASP.NET Core With MongoDB

Introduction

 
In this article, we will learn how to do CRUD operations with MongoDB using ASP.NET CORE. MongoDB is a NO SQL database. The advantage over the SQL-Server is scalability, agility, however, these are not the only redeeming qualities that database MongoDB possesses. 
 
Now we will start.
 
Step 1
 
Firstly, we will add the Mongo DB Library in Visual Studio using a Nuget Command
 
CRUD Operation Using Asp.net Core With Mongodb
 
Install-Package MongoDB.Driver -Version 2.11.6
 
Step 2 
 
Log in to mongodb
 
https://www.mongodb.com/
 
After logging in you create the cluster, leave it if you have already created one 
 
After creating the cluster, click on Connect 
 
CRUD Operation Using Asp.net Core With Mongodb
 
After clicking Connect, click on Connect your Application 
 
CRUD Operation Using Asp.net Core With Mongodb
 
After that, you will find the connection string.
 
CRUD Operation Using Asp.net Core With Mongodb
 
Select the drive, C#.NET and for the version, I am using 2.11.6, so select 2.11 or later.
 
Step 3
 
Now we come to Visual Studio. Create the ASP.NET Core Web App
 
CRUD Operation Using Asp.net Core With Mongodb
 
Step 4
 
After creating the project, go to appsettings.json and set the connection string.
  1. {  
  2.     "MyKey""mongodb+srv://ajay:<Password>@cluster0.qmqmt.mongodb.net/<Databasename>?retryWrites=true&w=majority",  
  3.     "Logging": {  
  4.         "LogLevel": {  
  5.             "Default""Information",  
  6.             "Microsoft""Warning",  
  7.             "Microsoft.Hosting.Lifetime""Information"  
  8.         }  
  9.     },  
  10.     "AllowedHosts""*"  
  11. }  
In the above MYKEY, you need to enter your information and change the password & database.
 
NOTE
If the database is already created then it's working, but if the database is not created, then it's created automatically.
 
Step 5
 
Now we comes to the Controller: 
  1. public HomeController(IConfiguration Configuration) {  
  2.     Client = new MongoClient(Configuration["MyKey"]);  
  3.     DB = Client.GetDatabase("Your Database");  
  4. }  
You can set your database name, which is the connection string.
 
Step 6
 
Now you create a class:
  1. public class CarModel {  
  2.     public ObjectId Id {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Empname {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string Empemail {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public string Phoneno {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public string state {  
  19.         get;  
  20.         set;  
  21.     }  
  22. }   
In the above class, ObjectId is Bson ID which is created automatically, and you use  
  1. using MongoDB.Bson;  
Step 7 - INSERT THE DATA
  1. [HttpPost]  
  2. public IActionResult Index(CarModel model) {  
  3.     var collection = DB.GetCollection < CarModel > ("EmployeeDetails");  
  4.     collection.InsertOne(model);  
  5.     return View();  
  6. }  
Step 8 - EDIT AND UPDATE THE DATA
 
TO EDIT 
  1. public ActionResult Edit(string id) {  
  2.     if (ModelState.IsValid) {  
  3.         var collection = DB.GetCollection < CarModel > ("EmployeeDetails").Find(Builders < CarModel > .Filter.Eq("Id", ObjectId.Parse(id))).SingleOrDefault();  
  4.         return View(collection);  
  5.     }  
  6.     return View();  
  7. }  
TO UPDATE 
  1. [HttpPost]  
  2. public ActionResult Edit(CarModel model, string Id) {  
  3.     var collection = DB.GetCollection < CarModel > ("EmployeeDetails");  
  4.     var update = collection.FindOneAndUpdateAsync(Builders < CarModel > .Filter.Eq("Id", ObjectId.Parse(Id)), Builders < CarModel > .Update.Set("Empname", model.Empname).Set("Empemail", model.Empemail));  
  5.     return RedirectToAction("About");  
  6. }  
In the Above EDIT, you will pass in your Current ID in ID Parameter.
 
Step 9
 
Show The DATA
  1. public ActionResult Show() {  
  2.     var collection = DB.GetCollection < CarModel > ("EmployeeDetails").Find(new BsonDocument()).ToList();  
  3.     return View(collection);  
  4. }  
Now we run the project.
 

Summary

 
In this article, we learned about Crud operations using ASP.NET Core and MongoDB. This article gives you a basic understanding of how we can use MongoDB with ASP.NET Core applications.