Introduction to MongoDB

MongoDB is a new database idea featuring Document Oriented Database concepts. In the rest of the article, we will see more about MongoDB, exploring its concepts, advantages, disadvantages, installation, manipulation and we will carry out a practical example of how to handle it.

Document Oriented Database

The general definition presented is that Document-oriented Databases use the concept of self-contained and self-describing data and documents, and this implies that the document itself already defines how it should be presented and what is the meaning of the data stored in its structure.

Document Oriented Database has the characteristic of containing all important information in a single document, being free of schemas, having unique universal identifiers (UUID), making it possible to consult documents through advanced methods of grouping and
filtering ( MapReduce ) and also allow redundancy and inconsistency.

These databases are also called NoSQL Banks (Not Only SQL). This NoSQL term is due to the absence of SQL, but this type of Database is not limited to just that, so the term is not the most correct for this new type of Database.

However, it is accepted because the term has already become popular in the community. Some came to defend the term NoREL (Not Relational), but unlike the previous one, this one was not very accepted. In summary, this type of Database does not bring with it the ideas of the relational model or the SQL language.

A fundamental difference between the two models arises when we need to create relationships in relational databases, unlike document-oriented banks that do not provide relationships between documents, which keeps their design without schemas. 

That way, instead of storing related data in a separate storage area, document databases integrate this data into the document itself. However, it is still possible to store related data separately, this can be done using a separate collection, however, care must be taken with this as performance gains may be lost.

There are several NoSQL databases today, the best known are MongoDB, DynamoDB, Azure Table Storage, Berkeley DB, Hadoop, Cassandra, Hypertable, Amazon SimpleDB, CouchDB, RavenDB, Neo4J, Infinite Graph, InforGrid, etc.

MongoDB Features

MongoDB is a document-oriented database, unlike traditional databases that follow the relational model.

Thus, we already have a first difference between the two models, where the document-oriented bank deals with documents and not with records as in the relational model where everything is represented using a two-dimensional approach (tables represented through two dimensions: rows and columns).

This Database has the characteristic of being an open-source code licensed by the GNU AGPL (Affero General Public License) version 3.0, having high performance, having no schematics, being written in C ++, multiplatform and being formed by a set of JSON applications. Although the MongoDB project started in 2007, the Database was only completed in 2009, thus launching the first version of MongoDB. Several languages ​​and platforms already have MongoDB drivers, including C, C #, C ++, Haskell, Java, JavaScript, Perl, PHP, Python, Ruby, and Scala. Besides, MongoDB has binaries for various platforms such as Windows, Mac OS X, Linux, and Solaris.

Among the companies that already use MongoDB are, SourceForge, FourSquare, MailBox (Dropbox email service), LinkedIn, SAP, MTV, Pearson Education, and many others. A list of all production services using MongoDB can be found here.

Installing MongoDB

To install MongoDB we must first download it, choosing an operating system version.

Now we can unzip the zip to some folder.

After that, we must go to the folder where we unzipped MongoDB and go to the “bin” folder. Finally, we run “mongod.exe”.

We can now run the MongoDB Shell client. The MongoDB Shell application is included with the distribution being located in the “bin” folder. On Windows, it is in the form of the mongo.exe application.

Through this Shell, we can create databases, documents, and collections. If you need help at any time, just give the “help” command on the Mongo Shell command line.

By default, the Mongo Shell connects to the “test” database. To switch to another database, we use the command “use bank name”. If the database does not exist, MongoDB will create it as soon as the data is included in it. The Shell should present the message: switched to DB meuMongoDB.

To create a document and store it in a new collection called “colors” we can use the command below in the Shell:

To check if the document was stored in the database, we execute the command below in the Shell:

Since the MongoDB Shell uses JavaScript, it is possible to write regular JavaScript constructs when interacting with databases.

Listing 1 creates a collection of character documents, each containing the representation of the string and its associated ASCII code.

Listing 1. Collection of documents

This loop will create 26 documents, one for each lowercase letter of the alphabet, each document containing the character itself, and its ASCII character code.

Using MongoDB

MongoDB uses JSON syntax that holds data using key/value pairs. Follows in Listing 2 is an example of creating a scheme:

Listing 2. Retaining data

The SQL equivalent would be like the code in Listing 3.

Listing 3. Retaining data in SQL

Then we would still have to create an INSERT to add information about the user.

In the first example above we have a document created in MongoDB that has three attributes: name, city, and state. As we can see, we have no definition of data types, the maximum size for each attribute, validation rules, or restrictions.

To store other data in the database, we execute the code below:

he MyData document could contain the contents of Listing 4 :

Listing 4. MyData Content

In the example above, we are storing the document “MyData” in the “meudb” bank.

We can also create different structures. Follows in Listing 5 Another way to store the above data:

Listing 5. MyData content in other storage.

We can also make an even more complex structure, like the one in Listing 6 :

Listing 6. MyData under other storage

We can see that there are no very strict validation rules, so we can store any type of document in the database. When we need to add a new attribute to the database, we can include this attribute only in the document where it is needed, different from the relational model in which a column applies to all records. This must be done carefully so that the database is not disorganized and with several lost attributes.

The interesting thing is that through an update MongoDB allows us to add data, this is because there is no scheme within the collections, that is, the collections can be dynamic.

MongoDB also allows data to be deleted using the command below :

Using a Relational Database would be equivalent to the code below:

Another interesting command for deletion that is widely used is simply to put the code in Listing 7 :

Listing 7. Delete Command

To update data with SQL we have the code in Listing 8 :

Listing 8 . Update command

In MongoDB, the equivalent would be according to the code in Listing 9.

Listing 9. Update command in Mongo

Another widely used command is for making queries. MongoDB has a powerful document-based query language, which makes the transition from a relational database to MongoDB quite easy, as queries are easily converted.

Follows in Listing 10 a query performed with SQL:

Listing 10. Query in SQL

Using MongoDB we would have the equivalent of Listing 11 :

Listing 11. Mongo consultation

We could also have numerical comparisons, as shown below:

Using MongoDB we would have the equivalent as below:

We could also have the “like” operator, which is widely used as shown in the following query

Using MongoDB we would have the equivalent as shown below:

Another very common operation is ascending and descending sorting in SQL as shown below:

Another very common operation is ascending and descending sorting in SQL as shown below:

To sort descending we would have SQL:

Using MongoDB we would have the equivalent as below:Another command widely used in SQL queries is join. Below we have an SQL query using a join and looking inside this table, below is the code:

Using MongoDB we would have the equivalent as below:

Making an analogy with the Relational Database we have that the JSON document would be the record, the collection would be the table, the indexes are the same and the embedding and linking would be the join.

The advantages of using MongoDB begin to appear when we can represent real-world objects as they are, and if new attributes arise, we can apply them only where necessary and not in all cases, as in the relational model.

While in the relational model we are obliged to think about avoiding data redundancy at all times, which is why relationships exist, in MongoDB we have a different situation in which there are no relationships and duplication is sometimes even encouraged as in the cases noted above where the data is stored the way we want it.

The idea of ​​MongoDB is that we have self-contained documents obtaining all the information we need without having to perform several joins, that way we only make one query and the return will be the entire document with all the information resulting in a significant performance gain.


Using MongoDB we have better performance since a single query returns everything we need to know about the document.

NoSQL databases have advantages over others when we need scalability, flexibility, handling massive amounts of data, good performance, and ease of query.

MongoDB has very simple queries to be performed since there are no transactions and joins. Queries are easier to write and easier to adjust.

Horizontal scaling with Sharding is very well implemented in MongoDB. Sharding is used when we have a lot of data and we are at the limit of the disk, so we divide that data between several machines, so we have more performance and greater disk storage capacity. Therefore, the more Shards the greater the storage and performance. MongoDB offers this option. For more details on Sharding, we can consult the official documentation on the official MongoDB page. Relational databases used a lot like MySQL do not support this type of solution by default, for that we would have to manipulate the data in a layer above the database, being much more work.

As the SQL query language is easy to convert to MongoDB we have an easier way to migrate organizations that currently use relational databases.

Finally, MongoDB also has a feature called GridFS that is responsible for storing large files. This means that videos, files, etc., can be stored directly in the database, unlike the relational model that we had to store everything in the Filesystem and provide only a reference for these files in the database. The response time of these files stored in MongoDB is compared to the response time of files in Filesystems.


A disadvantage is when we want to change all records related to a semantic unit, in which case it is necessary to treat them one by one.

A question to be answered is still the question that all users have in relation to the support of MongoDB that all large companies and high-value projects need. To what extent does MongoDB offer this necessary support? Another point concerns the availability of the service. These and other questions will be answered over time, the fact is that many large companies have used them in projects of medium complexity, a natural choice due to the short time that this new technology is on the market and the results have been very satisfactory.

Creating an Application with MongoDB

To exercise the knowledge acquired with MongoDB we will create an example application to manipulate a MongoDB database.

We must first download MongoDB at by choosing the version for our operating system.

After that, just unzip it in a folder or run the installer (if it is “.msi”).

Once this is done, we must create a “data” folder and inside the “data” folder a “db” folder in C: / Windows. In this folder, MongoDB will place the created databases.

Now we can run the application “mongod.exe” which is inside the “bin” folder of MongoDB. This application runs the MongoDB databases on port 27017. To manage the tables, we can use the MongoDB Shell by running the “mongo.exe” application.

We must also download the file “.jar” so that our Java project can communicate with the database. To do this, download the file and place it at the root of the project that will be created below.

Now we must go to Eclipse and create a new Java project, as shown in Figures

1 to 3.



Now that we have our Java project created, let’s take an example by creating a package called “com. example” and a class inside the created package.

Our class should look like the one in Listing 12.

Listing 12 . Example of connecting and manipulating data with MongoDB.

The code above creates a new database if one does not already exist with the name “testemongodb”, creates a new document with the name “data” and with the defined values, lists the inserted documents, and finally lists all the databases created so far.