Here is a simple example of how to use MongoDB with C#.

What is MongoDB?

MongoDB is a cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schema.

How to use the MongoDB?

Because MongoDB has to run on a server, you then have two options: install it or use an instance on the Cloud. I would suggest to follow this link if you want to install it locally or on your server. But according to me, the best option is to connect to MongoDB Atlas and create a free cluster (limited to 512 MB of Storage).

Simple tutorial using MongoDB with C#

Here we will try all the simple CRUD operations. The main advantage is that you don’t have to do anything to create your database or collections, they will be created automatically if they don’t exist yet.

First, open NuGet Managet and install MongoDB.Driver package, which will install all the dependencies you need.

Let’s start with the domain. We will store a simple entity called SettingInfo which basically contains a Name and a Value:

Use the MongoDB attribute to simplify your life! Otherwise you will have to manage the BSON documents manually and it’s kind of a nightmare, trust me…

MongoDB is pretty simple to use and really flexible. I strongly recommend you to at least try it.