Creating a Mongo DB based backend in ASP.Net Core API

Mongo DB

  • Document-oriented database 
  • Open source
  • Can be used to store large data
  • Quite easy to setup and use
  • Called NoSQL database which means not only SQL
  •  Retrieval & storage and of data in the MongoDB are not in the form of tables. 

How ASP.Net core connects to MongoDB

Assumptions:

1 – You have MongoDB installed. If not you can get it from Install MongoDB — MongoDB Manual

 2 – You have got the database setup and the tables added which you can get from our MongoDB based backend ASP.Net core project from our Github location.

This github project is a React + Redux + Asp.Net Core + JWT + MongoDb based ToDo application.

To learn about creating the whole application end-to-end you can refer our article – Creating a React app with Dotnet Core as backend and publishing on Azure 

Please open this project side-by-side to understand the flow better.  In the project you will find the json todo items like below:


3- To use MongoDB easily you should install MongoDB compass also. Which provide a very easy to use UI for MongoDB from Download and Install Compass — MongoDB Compass It looks like below, it has methods to Add table/add/update data in table etc.

4- If you are setting up a new ASP.Net core project then follow below steps to integrate MongoDB data source.

Steps to do inside ASP.Net Core application:

1 – Install MongoDB.Driver from nuget package source

2 – Set up connection string for the MongoDB in application.json file.

3- Create Server class to connect to the MongoDB. For example ToDoService and create method for Get/Put/Post/Delete there. 

 

In this ToDo application we have a User service which is being used by UserController and a ToDoService which is being used by ToDoController API .

One of the classes (ToDoApp) below, it connects withMongoDB via connection string given in appsettings.json

using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using ToDoApp;

namespace ToDoApp
{
  public class ToDoService
  {
    private readonly IMongoCollection toDoItems;

    public ToDoService(IToDoDatabaseSettings settings)
    {
        var client = new MongoClient(settings.ConnectionString);
        var database = client.GetDatabase(settings.DatabaseName);

        toDoItems = database.GetCollection(settings.ToDoCollectionName);
    }

    public List Get()
    {
        List ToDos;
        ToDos = toDoItems.Find(emp => true).ToList();
        return ToDos;
    }

    public ToDoItem Create(ToDoItem toDoItem)
    {
        toDoItems.InsertOne(toDoItem);
    }
    //   ... more methods....
  }
}          

To test the ToDo API you can use Postman app, but in the call you will need JWT token, to learn how JWT works in your calls, you can see the JWT token in Action section in our article JWT token implementation in ASP.Net Core 

Read more from Asp.Net C#:

Natalie Harris
With references from Jon Skeet
3.2k
Christopher Palmer
With references from Eric Nat
132
Christopher Palmer
With references from Peter Andrew
244
Natalie Harris
With references from Jon Skeet
290
Christopher Palmer
With references from Eric Nat
3.4k

Leave a Reply

Your email address will not be published. Required fields are marked *