How to write .Net Core Web API via Mongo DB on Windows Computer

Tolga ÇINAR
lTunes Tribe
Published in
4 min readOct 7, 2019

Hello everyone,

I will try to explain ‘How to develop Dotnet Core Web Api via Mongo DB in Windows Computer’ in this article. Firstly, we should install MongoDB. You can use as follows link for download.

Screenshot 1: MongoDB Download Page.

Let’s start to install.

Screenshot 2: Setup screen 1.
Screenshot 3: Setup screen 2.
Screenshot 4: Setup screen 3.
Screenshot 5: Setup screen 4.

You can cancel MongoDB Compass. I didn’t install it.

Screenshot 6: Setup screen 5.
Screenshot 7: Setup screen 6.
Screenshot 8: Setup result page.

Now, we will add new value in environment variables. This is for command prompt screen. Not about Dotnet Core app. We will use mongoDB setup location in this step. My one is C:\Program Files\MongoDB\Server\4.2\bin If you want, you can skip this step.

Screenshot 9: How to add Environment Variables.

We added two times. First one User variables for Your Username area and second one is system variables area. You should restart your computer after this step.

Let’s check MongoDB commands. We will just check some commands. First command is;

mongo

Screenshot 10: This command is answer for ‘How to start MongoDB’.

show databases

Screenshot 11: This command is for database list.

use databaseName

Screenshot 12: This command is for create a new database and use old ones. If you don’t add collections this new db can not create.

db.CreateCollection(“ExampleCollection”)

Screenshot 13: How to add collection and again check database list. New collection command is using with use databaseName command.

db.DropDatabase()

Screenshot 14: This command is answer for “How to drop current db”. This command is using with use databaseName command too.

That’s enough for now. We can check all commands in another article. Now, Let’s start to write a web api.

Screenshot 15: How to create web api project.

I created and opened my project. Now, I will add mongoDB driver in my project. I’m using this command for add. My command is as follows.

dotnet add package MongoDB.Driver --version 2.9.2

You can check your package in .csproj file.

Screenshot 16: Package Check.

We will start from Model folder for development. Firstly, I will create Base model file. Because mongoDB is using Id field in collections. I will create this file for base.

Screenshot 17: MongoDB Base Model.

Now, I will create an example model for my web api methods.

Screenshot 18: Car Model

Now, I will create a base repository for my models. I will use this with my model repository.

Screenshot 19: Base repository.

Example model repository as follows.

Screenshot 20: Example model repository.

We should add connection string property in appconfig.json file. We will use this in startup.cs file.

Screenshot 21: Appconfig file.

Startup.cs is as follows.

Screenshot 22: Startup.cs configuration.

Example methods are as follows.

Screenshot 23: Example methods.
Screenshot 24: Insert method check.
Screenshot 25: Get method check.

Now, I can see my database on command prompt.

Screenshot 26: Command prompt for database list.

You can download my example project in this link.

Best regards.

--

--