:::: MENU ::::

Using MongoDB with ASP.NET Core – Part I (Setup)

In this 2 part series, I will show you how to use MongoDB as data storage in your ASP.NET Core application.

As you may know, MongoDB is an open-source document database and leading NoSQL database. It is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on the concept of collection and document.

Note: Up to now MongoDB is not supported by EF Core. There is less chance that EF Core will support MongoDB ( based on a comment by Rowan Miller) but could be in future.

Now let’s get started. First, let’s set up an environment. Download and install MongoDB from here. The installation process is same as like other normal application. Check installed MongoDB binary in

C:\Program Files\MongoDB.

Now let’s keep the installation path of MongoDB applications on environmental variables so we can access from our Command prompt or PowerShell using mongo command later.


  • Get the path of MongoDB bin folder

  • Go to Control Panel >System and Security > System >Advance System Settings> Environmental Variables. There click path option of system variables and hit edit option and add the path of MongoDB (Which you just copied). The environmental variable window displayed below could be different on windows version before windows 10 10586 build. In that case, go to path option and hit edit there you will get a text box with existing variables there (in a text box) add a semicolon(;) at the end and paste the path and hit OK.

Now let’s create the folder to keep the data. In my case, I create it in E:\ drive with name Mongo\Employee. To work with MongoDB you must have to start MongoDB server first whether it’s in remote or on your local machine. To be more clear about MongoDB client and server of our local machine, go back to the bin folder of MongoDB you can see mongo and mongod .exe application which is a client(mongo) and server (mongod). To start Mongo server in power shell type mongod –dbpath E:\Mongo\Employee and hit enter and then mongo server will start and listen to the connection on port 27017. E:\Mongo\Employee is the path where we will keep our database. Here is how it looks like.

Now let’s see some database and add some data to it.

Start another instance of PowerShell (as a mongo client) and type mongo or mongo [db_name] then it will connect to mongo server and point to the database that you passed (if you try the second command). Type show dbs command to see the list of a database, by default there will be only localdb.

Now to insert data, try db.foo.insert({‘name’: ‘janak’}) it will insert document (as a row of SQL) with details name:Janak in foo collection (Like a table of SQL). Collection foo will be automatically created if it does not exist already. To see the data try db.foo.find() this will show your data of that collection (foo in this command) with random object ID (By default it is given by mongo but you can put your own object id to uniquely identify a document in a collection).

For that try db.foo.insert({‘_id’: 19,‘name’: ‘janak’}) and see the document by db.foo.find() there you can see 19 as an object id instead of random GUID no. You can see operations logs in another power shell where you run mongodb server earlier.

That’s all for the part I. So wrapping up, up to now our MongoDB setup is done. Both server and client are working fine. In next part, I will show how to perform CRUD in ASP.NET Core Web API project.

Happy Coding !!!