Enterprise Project Part 2: MongoDB

This is the continuation of my previous blog. In this blog I  will be demonstrating the setup using MongoDB and the RedisDB and work it together and to setup a strategy to work it together. Therefore I will cover the Web API Mongo and Redis is action.

Setup

So at first Mongo. Need to install the following NuGet package  from the NuGet library.

At the time of writing the version was 2.7.2. And MongoDB can be setup from https://www.mongodb.com locally. And also there is MongoDB compass to use as graphical interface. And when using NoSql, there  is a different way of mind set require to architect the database schema. And also in the code level. Unlike sql we do not need to run migration every time there a change in schema. In the projects appsettings.json, let add following lines

MongoDB’s default running port is 27017 and the database name is used as ‘Example’.

Need to create a class to work with the database which is simply

This class will be used to supply the connectivity across the project.

Let’s put following lines of code at the Startup.cs under ConfigureServices

Controller

In the controller let’s include following namespace

As we will be injecting the DbSetting, let’s define the following

So now need to pass the pass the DBSetting in the controller level when it initialize. And the code will be like following

Connecting

So in our solution we see everything is ready to go. Now let’s try to insert something into it. As I mentioned before, unlike sql we do not need to have a context setup to identify which are models. So every thing can be model and that is great. But also that can be a problem also. In a large team it can be a nightmare if codes and placements of files are not maintained correctly. Let make a class and use it to simulate Insert, Update, Delete.

But it is Mongo and we need to put a BSON in the class which we are using as database object. So the class will be like

and need to add following line in that class

In the MongoDB compass, we have created a database named ‘Example’ with a collection named ‘Person’

Collection

Insert

Now in any action of that controller action if we write

It will insert into the database

Pretty simple. But when working on project there need to be a sets of schema to define how this thing will be used.

Update

For update we need to have the identification of the object which we need to edit. Therefore we can use ObjectID for this. The following line will update the ‘firstname’ of the Person object.

After the UpdateOneAsync, the first portion is used to filter the object to update and the next  portion is the data telling the new values. It can be simplified using following

Delete

Writing the following line will delete the object

Conclusion

This article was at a glance about how to setup and make use of Mongo DB. But the clean architecture was not discussed. Also there is a need of choosing a pattern to get the ball rolling for an enterprise project.

Leave a Reply

Your email address will not be published. Required fields are marked *