Introduction to MongoDB

Wondering what MongoDB is? The need to analyse large amounts of data, as well as to maintain flexibility in their structures, has made the use of non-relational databases (NoSQL) increasingly common. An example of such a non-relational database is MongoDB, of which we are going to give a brief introduction in this post.

Throughout this publication we are going to tell you concepts to introduce MongoDB, from the most basic concepts to how to perform certain basic operations.

Basic concepts of MongoDB

The basic unit of data in MongoDB is called a document. Its structure is very similar to *JSON * but it is stored in a richer format known as * BSON * (Binary * JSON *). These documents are grouped in collections.

MongoDB is characterized by:

  • Schemaless collections
  • Horizontal scalability
  • High availability

Next we investigate in each one of them:

Schemaless Collections

In MongoDB, documents from the same collection do not necessarily share the same structure, which provides a great flexibility, perfect to make prototyping or projects where data models can vary iteratively. In the following example, we see two documents with different structure that are part of the same “jumpers” collection:

{                                  {
    color : "red",                    color : "blue",

    material : "wool",                material : "cotton",

    price : 20                        sport : true
}                                  }   

Horizontal scalability

MongoDB allows scaling horizontally in a simple way through a sharding system. This allows distributing collections between different machines (shards) so that, depending on the strategy we follow in choosing the sharding key, we can optimize readings or writings to handle higher operations volumes without losing response time.

High availability

MongoDB allows you in a simple way to configure replication systems in replica sets (sets form by N machines, a master and N-1 replicas), which guarantee that, in case of loss or fall of a master node, one of the replicas is automatically promoted to master, maintaining the operating system

But not everything are advantages

One of the main cons in MongoDB is the lack of transactions as we know them in related databases. There are only transactions in the operations on a single document, at a very low level. This is what is known as atomicity.

Another disadvantage is the complexity in the construction of queries since being a non-related database, the concept ** JOIN ** does not exist in information recovery statements.

Basic operations (CRUD – Create, Read, Update, Delete)

There are many operations that can be done in MongoDB. Some of the most essential are to insert a new document (create), obtain a document (read), update an existing document (update) and delete a document (delete).

We give you a case. Suppose that our schema in the database is called ‘wardrobe’, and it contains a “jumpers” collection  to store our previous documents. Before any operation in the terminal, we have to make sure we have selected the scheme in which we want to work:

use wardrobe

How to insert a new document (Create)

To insert a new document, we will execute the * insert * instruction on the collection, indicating the attributes that make up the document in the following way:

    color : "red",  
    material : "wool",
    price : 20   

This instruction can also receive an array of documents, inserting them at the same time:

    color : "red",  
    material : "wool",
    price : 20        
    color : "blue", 
    material : "cotton",
    sport : true        

As of version 3.2, there are separate instructions to add documents or a single record (insertOne), or multiple ways to receive an array (insertMany).


How to obtain a document (Read)

To search for documents, we use the operation “find”, combining it with a series of operators that allow us to configure the search filter. In the following example, we look for jerseys with a price greater than 10 (operator * $gt *):

db.jumpers.find( { price: { $gt : 10 } } )

If we also want to order them by price in ascending order, we would apply a criterion to the set already filtered by the operation * sort *:

db.jumpers.find( { price: { $gt : 10 } } ).sort( { price : 1 } )

How to update an existing document (Update)

To update a document, we will execute the ‘update’ instruction on the collection, first configuring the filter to be applied on the total data set and then the attributes to be edited.

db.jumpers.update({ color : "red" }, { $set : { price : 25 } } )

This instruction updates the first document that it finds that meets the criteria. If we wanted to modify all the documents that fulfilled this condition, we would have to indicate it with the multi flag:

db.jumpers.update({ color : "red" }, { $set : { price : 25 } }, { multi : true } )

It is important to note that if we want to edit only some fields of a document, we have to indicate it through the $set operator, since if we ignore it, we will be replacing one document with another:

/ * The structure of the document would be {color: "red", material: "wool", price: 25} * /
/* La estructura del documento quedaría { color : "red", material : "wool", price : 25 } */
db.jumpers.update({ color : "red" }, { $set : { price : 25 } } )

/ * The structure of the document would be {price: 25} * /
db.jumpers.update ({color: "red"}, {price: 25})

As of version 3.2 there are separate instructions for updating a document (updateOne) or multiple ways (updateMany).

How to delete a document (Delete)

To delete a set of documents from a collection, we will execute the instruction ‘delete’ indicating the filter that all the documents that we want to eliminate must meet:

db.jumpers.remove({ color : "blue" })

Contrary to the previous operations, in which by default only on the first document that fulfills the condition is acted upon, in the case of ‘delete’ all the documents that comply with it will be eliminated. If we wanted to delete only one document, we should indicate it by justOne flag:

db.jumpers.remove ({color: "blue"}, {justOne: true})

As of version 3.2, although the delete operation can still be used, there are two other instructions to delete a single document (deleteOne) or delete multiple (deleteMany).

MongoDB is a non-related database that offers us power, flexibility and a good number of features ready to be used simply by installing it. However, the choice of one type of database or another will always depend in the needs and characteristics of our use case. See you next week!


    La mejor solución de firma electrónica para tu empresa

    Scroll to Top