Within the NoSQL databases, probably one of the most famous is MongoDB . It is very different from the relational database concept and it is becoming an interesting alternative.
learning-MongoDB-starting
But when one starts in MongoDB, one can feel lost. We have tables, we have no records and more importantly, we have SQL. Still, MongoDB is a serious candidate to store the data of our applications.

In this article we will explain how NoSQL data base works, what can we do with it and how can we do.

What is MongoDB?

MongoDB is a database-oriented documents. This means that instead of storing the data in records, it stores data in documents. These documents are stored in BSON, a binary representation of JSON.

One of the most important differences with respect to relational databases is that you need not follow an outline. The documents of the same collection – similar to a table in a relational database concept – may have different schemas.

Suppose we have a collection we call people. A document may be stored as follows:

{
 
Name: "siddique"
 
Surname: "shabina"
 
Age: 22
 
Hobbies: ["blogging", "reading", "drawing"]
 
Friends: [
 
{
 
Name: "Zaara",
 
Age: 10
 
},
 
{
 
Name: "Mantasha"
 
Age: 16
 
}
 
]
 
}

The above document is a classic document JSON. It has strings, arrays, and numbers subdocuments. We could save a document like this in the same collection:

{
 
Name: "Ayisha"
 
Class: "MA"
 
12 friends
 
}

This document does not follow the same pattern as the first. It has fewer fields, a new field that does not exist in the earlier paper and even a field of a different type.

This is unthinkable in a relational database and is totally valid in MongoDB .

How MongoDB work?

MongoDB is written in C + +, although consultations are objects passing JSON as a parameter. It’s quite logical, since the documents themselves are stored in BSON. For example:

db.Clientes.find ({name: “Peter”});

The above query finds all customers whose name is Pedro.

MongoDB comes with a console from which you can execute various commands. This console is built on JavaScript, so that queries are made using this language. Besides the functions of MongoDB, we can use many of the functions JavaSciprt own. In the console, we can also define variables, use loops or functions.

If you want to use your favorite programming language, there are drivers for a large number of them. There are official drivers for C #, Java, Node.js, PHP, Python, Ruby, C, C + +, Perl or Scala. Although these drivers are supported by MongoDB, it does not mean that everything else is in the same state of maturity. For example, C is an alpha release. If we use a particular language, it is best to review the available drivers to check their suitability for a production environment.

Where can I use MongoDB?

Although they say that NoSQL databases have a narrow field application, MongoDB can be used in many of our projects today.

Any application that needs to store semi-structured data can use MongoDB. This is the case of the typical applications CRUD or many existing web development.

However, while the collections of MongoDB does not need to define joins scheme, it is important that we design our application to do one. We think if we need to normalize the data, denormalizarlos or use a hybrid approach. These decisions can affect the performance of our application. Ultimately, the schemes define queries that we will make more often.

MongoDB is particularly useful in environments that require scalability. With sharding and replication options that are simple to set up, we can get a system that scales horizontally without too much trouble.

Where not to use MongoDB?

In this database there are no transactions. Although our implementation can use any technique for simulating transactions, MongoDB does not have this capability. It guarantees atomic operations only at the document level. If transactions are indispensable in our development, we think of another system.

Nor does the JOINS. To view two or more related data collections, we have to do more than one query. In general, if our data can be structured in tables, and we need relationships, you better opt for RDBMS classic.

And finally, there are the aggregation queries. MongoDB has a framework for querying this guy named Aggregation Framework. You can also use Map Reduce. Still, these methods do not reach the output of a relational system. If we will need to exploit complex reports, we think of using another system. Yes, this is a gap that MongoDB will cut with each release. This might not be a problem back in the days.

How to install MongoDB?

Installing a server instance is a cinch. We simply have to download the binary for your operating system. There are two versions of Windows, Linux and MacOS. Once downloaded, we can start the MongoDB service with a single command.

mongod - dbpath data

With this command we start the mongoDB service, which will start to hear requests on port 27017. It is important to specify the parameter -dbpath, with the path where the files of our database is stored.

How I can use the console?

If we have already released our server machine, simply launch the console the following command

mongo localhost

If you go into the console, you can write commands. If we write help, we will have a list with the most common commands and their description.
And when we leave here,  we will see how you can do inserts, queries and updates data in future articles.