17th Jan 2022

MongoDB With .NET 5


Written By, Divya Trivedi


Hello Devs,
As we know there are many platforms available for Storing Databases for .NET Applications. In this blog, we are going to discuss MongoDB with .NET 5. We will learn why MongoDB is preferred, and why it’s best for storing data in MongoDB.

Introduction of MongoDB

MongoDB is a NoSQL database management program. We can say MongoDB is a tool that can retrieve and store data from the MongoDB database.

Why MongoDB?

MongoDB is representing information as a series of JSON.

MongoDB provides fast performance for storing data on databases and supports the feature insertMany() API for Bulk inserting data. We can say MongoDB provides fast performance for inserting and updating data on databases.

MongoDB with .NET 5

There are many reasons for using MongoDB with .NET, Let’s look at MongoDB with .NET 5.

.NET provides the NuGet package and supports all MongoDB Operations using MongoDB.Driver package.For Example

  • Create Database
  • Insert
  • Update
  • Delete
  • Retrieve
  • Bulk Insert, Update, Delete
  • and many more Operations

CRUD Operations

So, We are going to create an API that performs CRUD operations on MongoDB.

In this Blog, we learn how to

  1. Creation of .NET Project
  2. Installation of NugGet Package
  3. Use of ConnectionString
  4. Creation of database
  5. Define a MongoDB collection and schema
  6. Perform MongoDB CRUD operations

Steps For creating .NET Project

  • Go to File – New – Project.
  • After creating the ASP.NET Core Web Application project, and select Next.
  • Name the project generic. api, and select Create.
  • Select the .NET Core target framework and ASP.NET Core 5.0. Click on the API template, and select to create a project.

Installation of NugGet Package

  • Visit the NuGet package Gallery: MongoDB.Driver and install the stable release of MongoDB.Driver package.
  • After installing the package go to Configure MongoDB.

Use ConnectionString


  • So we can show The above image our connection to string, which will be our ConnectionString for our demo example.
  • Set ConnectionString In the appsetting file.
  • For that write ConnectionString as follows
    “ConnectionStrings”: {“Default”: “mongodb:// utMS=1000” }

Create database

Create one common file in the controller folder for defining connection and creating the database. For that, we need to create “CommonController.cs” in that we create a Database name as GenericDb and use ConnectionString that is set in Default.

Now we will use our CommonController on our every created API

Define a MongoDB collection and schema

  1. Schema Creation
    we need to create an entity of schema in the model Folder, Here we give the schema name as Employee.
  2. For Collection
    Before creating an API, we need to create a Collection so that we create the following Constructor.

    So our schema and Collection are ready. Our collection name is an employee. It’s stored and fetched data using the entity name Employee.

Perform MongoDB CRUD operations

Now time to create the first API, our first API will be inserted. Let’s see how to create an Insert API.

  1. Insert API
    Using the OnsertOne() method of MongoDB we can insert one record at a time in the database like below Code example




  2. Retrieve API
    We are going to create a simple Get API for Retrieve Employee List using AsQueryable() method of MongoDB


  3. Update API
    Here we are going to update eName By its empNo, Let’s see how it works.




  4. Delete API
    In Delete API we are going to Delete one record by employee Id because MongoDB provide By Default Primary key on the Id column so here requiredIdRequest is for our Id request



    So, Here Output is Null because there is no record found in the employee collection.


So after going through MongoDB with .NET 5, we can say MongoDB stores data faster than any other database. Providing fast performance and less code of C#, we can perform operations using MongoDB’s default methods.

Written By,

.NET Developer at Yudiz Solutions Pvt. Ltd