MongoDB document oriented database | introduction

We live in the world where we are bombarded with huge amount of information, news and advertising. Computer network becomes the central place of business. For this reason databases become fatter and fatter.

Mateusz Starzyk

|

24 Nov
2017

Few words about document oriented databases

We live in the world where we are bombarded with huge amount of information, news and advertising. Cultural and social life, newspapers, TV are transferred to the network. Computer network becomes the central place of business. For this reason databases become fatter and fatter.

Computer systems are facing a challenge of storing a lot of different information and to ensure the maximum short time to access to them. As we all know, in fact the customer/user cannot wait long.

Relational databases, which are with us over 40 years, on one side are very well optimized and tested but on the other hand - they stopped being enough to meet all the expectations of the software developers.

Scalability, performance and cloud computing are the key concepts which relational databases do not handle well with. The dispersion of data between multiple servers became very difficult problem.

Software developers needed also to face the challenge which is "change". The world is changing very fast and at the same rate (or even faster) computer systems must also change. Agile methodology requires flexibility, making changes in easy way and speed of reaction. These factors are difficult or often even are not possible for achieving in relational databases, especially in case of very complex and complicated data structures.

In response to the needs of the market a new approach to the storage of data appeared: Document oriented databases. The data is stored in looser and natural way, without restrictions related to the scheme. The data is stored in the forms of "documents" which are independent data units.

This approach allows to separate data between servers without loss of efficiency, changes in the structure of the data are also very simple and access to data is very fast.

Of course such approach also have some disadvantages. The data structure is not verified in any way - for this reason keeping a unitary data structure is very heavy. Because of that document oriented databases is very often used in case of unstructured data and for structured data the systems rely on relational databases. Another important limitation is transactional, which in case of document oriented databases can be maintained only at the level of the document or the entire database.

Data representation in MongoDB

In MongoDB data is stored in the form similar to JSON object named BSON. Documents are grouped into collections. We can say that document is a single row (compare to a relational database) and collection is a table.

Quick start with MongoDB using JAVA

In this part of the article I will show some basic operations (CRUD operations) on MongoDB using JAVA. To start we need to install MongoDB Community Edition. Installation manual depends on our operation system that we can find on MongoDB website: https://docs.mongodb.com/manual/installation/

If we want to use Java to work with MongoDB we need to add MongoDB JDBC driver to our program:

This default constructor creates for use connection on default port 27017 with localhost url. If we changed MongoDB server settings we use one of different constructors from MongoClient class, for example:

MongoClient mongoClient = new MongoClient("localhost", 27017);

If we successfully connect to the server we can list all existing database:

D like Delete

The last operation from CRUD is Delete. For this operation we also have more than one possibility. Similarly like in case of update, for delete we also have deleteOne (only one document will be removed, even if there is more than one document that match the criteria) and deleteMany (all documents that match the criteria will be removed from collection).

Summary

I hope this article gave you a few important details about the document oriented databases and encouraged to the further explore possibilities MongoDB. In fact, there are much more possibilities than I was able to mention here.