How to integrate MongoDB in ASP .Net Core Web API

In this course you will learn how to integrate mongodb in asp net core web api. First we configure and setup MongoDB for .Net then we will do the .Net Part inside Visual Studio.

I also have a full course on YouTube about integrating MongoDB in ASP .Net Core Web API. The course is a full stack CRUD Application Project with React.js, MongoDB and Web API so you learn React and Web API as well.

You can watch the full course or the video tutorial Here

In just 9 steps you can configure and setup Web API for making successful API calls to MongoDB Cloud Database:
  1. Configure MongoDB Cluster.
    1. Create a new Project if you have not created yet (You can also use any other projects).
    2. Click on the project that you want to connect to Web API then create a cluster by clicking on The big button "Build a Database":
      1. Select the correct options
      2. Select the correct provider
      3. Select the correct region
      4. Give a good Cluster name
      5. Give a good username and password
      6. Select the correct environment type
      7. Add the correct IP address. It also automatically detects the IP address of the machine with which you configure MongoDB But if you use the MongoDB in another machine then you have to add the IP address of that machine as well.
    3. Click on the "Connect" button inside your MongoDB Cluster to configure the MongoDB Cluster
      1. Select the driver(C# .Net)
      2. Select the latest version
      3. Copy the connection string and replace the "<password>" with the actual user Password
    4. Create Database
      1. Click on the tab button "Collections"
      2. Click on the button "Add My Own Data"
      3. Give a Database name and Collection name (You need these names later when you setup Web API)
      4. When you have Database and collection(basically Table), you can add fields(columns) to your collection or table.
      5. By Default your collection has only on field(that is the "_id")
      6. Add new fields by clicking on "Insert Document"
      7. Choose the correct data type for your fields
  2. Now you are done in MongoDB, let's jump to the Visual Studio
  3. Create a new project if you do not have any(it can be only Web API project or combination of Web API and other types like MVC or React.js frontend,...)
  4. Add a new object inside "appsettings.json" to store the MongoDB data:

  5. Add middleware for mongodb in "Program.cs":

  6. The Code For "DatabaseSettings.cs":

  7. The Code For "Student.cs". Student is for example the name of your collection inside MongoDB and based on your collection you need to create a model in Web API:

  8. The Code For "StudentServices.cs". Student is for example the name of your collection inside MongoDB. It is best practice to keep everything separated(one service for one collection):

  9. The Code For "StudentController.cs". Student is for example the name of your collection inside MongoDB. It is best practice to keep everything separated(one controller for one collection):