Creating .NET Core Console Application with NoSQL- MongoDB At Back-end
Here we will be demonstrating how to create a very simple Microsoft .Net Core Console Application using NoSQL – MongoDB at back-end.
Prerequisite
- Here we will be using Visual Studio Update 3.
- We have to install MongoDB instead of SQL Database.
Steps for the application
Step 1:
- Create a folder named ‘data’ in C: drive and ‘db’ within ‘data’ folder to keep our database.
Step 2:
- Now we will start the MongoDb instance. For this let’s go to C:\Program/Files/MongoDB\Server\3.2\bin.
- Now open its bin in command prompt for example cd C;\ Program \Files\MongoDB\Server\3.2\bin. If we have version 3.4, our path we be
- C:\Program\Files\MongoDB\Server\3.4\bin>mongod.exe –dbpath C:\data\db
- Our MongoDB Server is now up and running. We need to keep the Command Prompt open till the end. Open Mongobooster and go to create and connection. Mongobooster àConnect àCreateàSaveà Connect. This will connect us with Mongo Server.
- Right Click on localhost i.e. Server on left panel and create a database named ‘Education’. Then, right click on education and create Collection. This collection is nothing but our table in RDBMS. Just name it as ‘EducationDetails’. To check if everything is on track right click on EducationDetails and select to view document. There will only be query but not the records in the window.
- Now we are ready with back-end, In the Visual Studio >>File>>New>> Project>> Console Application >> Create.
- Right Click on reference to get Nuget Package Manager to get the driver to get connectivity with MongoDB. In “Browse”, search for MongoDB.Driver driver and install it. For this example, Here we are using version 2.4.3.
- Replace the below code with that in our Program.cs file
using System;
//MongoDB.Driver
using MongoDB.Bson;
using MongoDB.Driver;
namespace AppTest
{
public class Students
{
public ObjectId Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string Age { get; set; }
}
public class Program
{
protected static IMongoClient _client;
protected static IMongoDatabase _database;
public static Students GetStudent()
{
Console.WriteLine("Please enter student first name : ");
string FNm = Console.ReadLine();
Console.WriteLine("Please enter student last name : ");
string LNm = Console.ReadLine();
Console.WriteLine("Please enter student age : ");
string StudentAge = Console.ReadLine();
Console.WriteLine("Please enter city name : ");
string StudentCity = Console.ReadLine();
Students student = new Students()
{
FirstName = FNm,
LastName = LNm,
Age = StudentAge,
City = StudentCity,
};
return student;
}
public void CRUDwithMongoDb()
{
_client = new MongoClient();
_database = _client.GetDatabase("School");
var _collection = _database.GetCollection<Students>("StudentDetails");
Console.WriteLine("Press select your option from the following\n1 - Insert\n2 - UpdateOne Document\n3 - Delete\n4 - Read All\n");
string userSelection = Console.ReadLine();
switch (userSelection)
{
case "1":
//Insert
_collection.InsertOne(GetStudent());
break;
case "2":
//Update
var obj1 = GetStudent();
_collection.FindOneAndUpdate<Students>
(Builders<Students>.Filter.Eq("FirstName", obj1.FirstName),
Builders<Students>.Update.Set("LastName", obj1.LastName).Set("City", obj1.City).Set("Age", obj1.Age));
break;
case "3":
//Find and Delete
Console.WriteLine("Please Enter the first name to delete the record(so called document) : ");
var deletefirstName = Console.ReadLine();
_collection.DeleteOne(s => s.FirstName == deletefirstName);
break;
case "4":
//Read all existing document
var all = _collection.Find(new BsonDocument());
Console.WriteLine();
foreach (var i in all.ToEnumerable())
{
Console.WriteLine(i.Id + " " + i.FirstName + "\t" + i.LastName + "\t" + i.Age + "\t" + i.City);
}
break;
default:
Console.WriteLine("Please choose a correct option");
break;
}
//To continue with Program
Console.WriteLine("\n--------------------------------------------------------------\nPress Y for continue...\n");
string userChoice = Console.ReadLine();
if (userChoice == "Y" || userChoice == "y")
{
this.CRUDwithMongoDb();
}
}
public static void Main(string[] args)
{
Program p = new Program();
p.CRUDwithMongoDb();
//Hold the screen by logic
Console.WriteLine("Press any key to trminated the program");
Console.ReadKey();
}
}
}
- Now run the console application. We can perform the CRUD operation with .NET Core and MongoDB.