In this article, we will learn about working with MongoDB in ASP.NET Core. We will be building a WebAPI Project that can manage customer data. Along the way we will cover various concepts including Introduction to NoSQL Database, What is MongoDB, Installing MongoDB Locally and on the Cloud, Integrating with ASP.NET Core and much more. In general, you will be getting a complete working knowledge with MongoDB and probably set enough to start implementing MongoDB in your next ASP.NET Core Project. The source code of the application that we will be building is available here.

We will be working with ASP.NET Core 3.1 WebAPI, Visual Studio 2019 IDE and MongoDB. Let’s begin!

Table of Contents

What are NOSQL Databases?

If you are new to NoSQL Databases, it mean exactly what it sounds like. NOSQL or Non SQL Databases contrary to the SQL Databases, store the data in forms that not in any way similar to Relational Tables. A very common misconception is that, NOSQL Databases cannot store relationship data. It can store the relationship data as well, but just in a different way that is nothing similar to how SQL Databases works.

Querying a SQL Database may be complicated for specific usecases. Imagine having to search for a particular user data that is available only after Joining Multiple Tables. If the number of records are high, the query performance may be on the weaker side at times. With NOSQL Databases, you get to store all the user data under one document / dictionary. Thus, while querying, the application has to just look under one document. This can noticabley improve the performance.

Note that, this doesnt mean SQL Databases is no longer recommended. You could have both SQL and NOSQL Databases under a single application. An example of such an use case is , You could NOSQL Databases as a secondary Database that can be used to Search purposes as we know that it can outperform SQL DB.

The advantages with using NOSQL Databases are as follows.

  1. Scalable
  2. Affordable and Easy to setup.
  3. Quick
  4. Flexible
  5. Lightweight

Now, there a quite a lot of types of NOSQL Databases. This includes the following

  1. Document Database – These databases allows you to store data in the form of JSON objects. MongoDB is the most popular Document Database out there. It can be used to store a large amount of complex data which can in turn be easily queried without any compromise to the performance / response times. We will be talking extensively on MongoDB in this article.
  2. Graph Database – A more complicated form of NOSQL DBs which stores data in nodes and edges.
  3. Dictionary Databases – Redis is a great example of Key-Value Database. Such Database are not very complication. They just have a key and value. This is an excellent choice when you want to store tons of data that are very simple like user preferences, application language texts, country informations. Caching is a perfect use case of such databases. You get the point, yeah?

#coding

Working with MongoDB in ASP.NET Core - Ultimate Guide
1.25 GEEK