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
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
After clicking Connect, click on Connect your Application
After that, you will find the connection string.
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
Step 4
After creating the project, go to appsettings.json and set the connection string.
- {
- "MyKey": "mongodb+srv://ajay:<Password>@cluster0.qmqmt.mongodb.net/<Databasename>?retryWrites=true&w=majority",
- "Logging": {
- "LogLevel": {
- "Default": "Information",
- "Microsoft": "Warning",
- "Microsoft.Hosting.Lifetime": "Information"
- }
- },
- "AllowedHosts": "*"
- }
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:
- public HomeController(IConfiguration Configuration) {
- Client = new MongoClient(Configuration["MyKey"]);
- DB = Client.GetDatabase("Your Database");
- }
You can set your database name, which is the connection string.
Step 6
Now you create a class:
- public class CarModel {
- public ObjectId Id {
- get;
- set;
- }
- public string Empname {
- get;
- set;
- }
- public string Empemail {
- get;
- set;
- }
- public string Phoneno {
- get;
- set;
- }
- public string state {
- get;
- set;
- }
- }
In the above class, ObjectId is Bson ID which is created automatically, and you use
Step 7 - INSERT THE DATA
- [HttpPost]
- public IActionResult Index(CarModel model) {
- var collection = DB.GetCollection < CarModel > ("EmployeeDetails");
- collection.InsertOne(model);
- return View();
- }
Step 8 - EDIT AND UPDATE THE DATA
TO EDIT
- public ActionResult Edit(string id) {
- if (ModelState.IsValid) {
- var collection = DB.GetCollection < CarModel > ("EmployeeDetails").Find(Builders < CarModel > .Filter.Eq("Id", ObjectId.Parse(id))).SingleOrDefault();
- return View(collection);
- }
- return View();
- }
TO UPDATE
- [HttpPost]
- public ActionResult Edit(CarModel model, string Id) {
- var collection = DB.GetCollection < CarModel > ("EmployeeDetails");
- var update = collection.FindOneAndUpdateAsync(Builders < CarModel > .Filter.Eq("Id", ObjectId.Parse(Id)), Builders < CarModel > .Update.Set("Empname", model.Empname).Set("Empemail", model.Empemail));
- return RedirectToAction("About");
- }
In the Above EDIT, you will pass in your Current ID in ID Parameter.
Step 9
Show The DATA
- public ActionResult Show() {
- var collection = DB.GetCollection < CarModel > ("EmployeeDetails").Find(new BsonDocument()).ToList();
- return View(collection);
- }
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.