Introduction to MongoDB

NO SQL databases

The conventional databases are relational, that is, they consist of tabular lists of data (also known as “data relationships”, hence the true origin of their name), which relate to each other to store in the most efficient information.

In recent years, the Internet has grown dramatically resulting in new types of applications that need to manage huge amounts of data, which must be introduced and extracted quickly and should be able to scale / grow easily and cheaply. In these systems, flexibility, speed and horizontal scalability are more important than other traditionally crucial issues such as consistency or having a perfectly defined structure for the data.

Therefore, there is an unstoppable tendency towards the definition and use of a new type of documentary type database. Its main defining characteristics are, on the one hand, the enormous flexibility that they provide in the data schema when storing the Information and its simplicity of use, but also its very high performance and scalability, ease of maintenance and the ability to operate without failure points as they can recover even if any of their nodes fall.

In addition, the way of querying and extracting and summarizing information is completely different from what we are accustomed to with SQL, so this type of documentary data is also called non-relational, documentary or more commonly NonSQL (Not Only SQL) databases.

Introduction to MongoDB

MongoDB is a distributed document-type NoSQL data manager that stores documents in a format similar to JSON (to be more accurate internally uses BSON). It is written in C ++ and is multi-platform, Open Source and free.

Its main characteristics are:

  • Flexible storage based on JSON without having to define schemes previously.
  • Support for creating indexes from any attribute, which makes it very easy to use because it is not necessary to define Map-Reduce processes.
  • High performance for queries and updates.
  • Flexible document based consultations.
  • High capacity for growth, replication and scalability: you can scale out simply by adding inexpensive machines without affecting performance or complicate management.
  • Support for independent storage of files of any size based on GridFS.
  • Support for Map-Reduce tasks if necessary.

These types of databases do not replace traditional databases, such as SQL Server, Oracle or MySQL, but complement them for certain types of specialized applications. Documentary databases such as MongoDB are used for a multitude of tasks, but fundamentally when we need flexibility in defining the data, simplicity in accessing them, great performance and the possibility of growing very fast.

It is suitable for creating Internet applications that record a lot of data or that we simply want to create very flexibly, but also for very large systems such as sensor data loggers, which can receive tens or hundreds of thousands of data readings per second , Through sales data managers, storage infrastructure for social networks, mass online games, content managers, data analysis applications and reporting.

Being JavaScript-based, it is especially good with all types of web applications, especially the most modern Single Page Applications.

In general MongoDB can be used for almost anything for which you would use SQL Server or MySQL, but without the rigidity that these types of databases present.

CRUD Example

We will build an application that inserts, queries, deletes and reads rentals.

First we need to download MongoDB in our computer, to do this we follow the following steps:
1. Download MongoDB from the following link:
2. Go to the MongoDB server folder and create a folder called “data” where our database files will be created.
3. Go to the bin folder open a console to run the following command to run the database: mongod –dbpath ../data

Once the MongoDb is install we create a new Console application in Visual Studio and install the official MongoDB.Driver using Nuget.

Then we need a MongoDB client to interact with the server.

MongoClient dbClient = new MongoClient("mongodb://localhost");

Now we can start executing some commands, in this case we will list all the database present on the server.

//Get Database and Collection
IMongoDatabase db = dbClient.GetDatabase("test");
var collList = db.ListCollections().ToList();
Console.WriteLine("The list of collections are :");
foreach (var item in collList)

Let’s try to perform CRUD operations. As I already mentioned, JSON is the preferred input/output format but the documents are stored in BSON (Binary JSON) format in the database. So, here we will be using BsonDocument class object to store the data.


var people = mongoDatabase.GetCollection<BsonDocument>("People");

 BsonElement firstNameElement = new BsonElement("FirstName", "Gabriel");

//Method 1
BsonDocument personDoc = new BsonDocument();

//Method 2
personDoc.Add(new BsonElement("Age", 50));

In this snippet, we retrieve the current collection first. Next, we create a BsonDocument object where we want to store our data.

In Method 1, I showed how to explicitly create a BsonElement object variable to store key-value pair and then add it to the BsonDocument object.

In Method 2, I did not create a BsonElement object variable, rather I directly passed the object as parameter. The last statement inserts the data in the collection “people”.


var resultDoc = people.Find(new BsonDocument()).ToList();
foreach (var item in resultDoc)


BsonElement updateFirstNameElement = new BsonElement("FirstName", "Pedro");

BsonDocument updatePersonDoc = new BsonDocument();
updatePersonDoc.Add(new BsonElement("Age", 80));

BsonDocument findPersonDoc = new BsonDocument(new BsonElement("FirstName", "Gabriel"));

var updateDoc = people.FindOneAndReplace(findPersonDoc, updatePersonDoc);


BsonDocument findAnotherPersonDoc = new BsonDocument(new sonElement("FirstName", "Pedro"));

You can find this example HERE

Example using ASP.NET MVC and MONGODB Database

In this demo I have created a ASP.NET MVC Application to manage Rentals. In this example I use GRIDFS to store big documents and photos inside the mongo Database.

You can find the code of this example here

Posted in Databases | Tagged , | Leave a comment

State Design Pattern

The purpose of the State Design Pattern is allow an object to modify its behaviour when its internal state changes. Although this can often be solved with a boolean or using constants, this usually ends up with a lot of if-else, unreadable code and difficulty in maintenance.

Continue reading

Posted in Architecture | Tagged , | Leave a comment

Adapter Design Pattern

The adapter pattern is definitely one of the most used design patterns in software development. As per GOF guys, Adapter pattern is defined as the following:  “Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.”

Continue reading

Posted in Architecture | Tagged , | Leave a comment

Strategy Design Pattern

Another day, another Design Pattern, in this article I will explain the Strategy Design Pattern.

The Strategy pattern allows you to define an algorithm separately and select the algorithm dynamically at run time. This pattern defines a set of algorithms, encapsulates each one of them and makes them interchangeable. It allows the algorithm to vary independently of the clients that use it.

Continue reading

Posted in Architecture | Tagged , | Leave a comment

Façade Design Pattern

The Façade Pattern it is a very simple pattern, it seeks to simplify the system, from the point of view of the client, by providing a unified interface for a set of subsystems, defining a higher level interface. This makes the system easier to use. The GOF book says the Facade Pattern should “Provide a unified interface to a set of interfaces in a system. It defines a high level interface that makes the subsystem easier to use”.

Continue reading

Posted in Architecture | Tagged , | Leave a comment

Template Design Pattern

The Template Method design pattern is part of the so-called behaviour pattern family. These patterns help solve problems of interaction between classes and objects. This pattern arises from the need to extend certain behaviours within the same algorithm by different entities. That is, different entities have a similar behaviour but differ in certain specific aspects depending on the particular entity.

Continue reading

Posted in Architecture | Tagged , | Leave a comment

Setup SonarQube for C# Projects

Recently I have started to use SonarQube and I have to say that I like it. In this post I will talk about this tool and the process of installation and configuration. My objective is to analyse C# projects.Setup_SonarQube_for_ C# _Projects_img1

SonarQube is an open source platform for continuous inspection of code quality. It  uses various static source code analysis tools like Checkstyle, PMD or FindBugs to obtain metrics that can help improve the quality of our programs’ code.

Continue reading

Posted in Continuous Integration | Leave a comment