by ravikiran perumalla

Tag: java mongoDB

We keep hearing about NoSQL these days. Many of us might think what’s the big deal if there is no “SQL”. Some of us might think OMG, there is no SQL ? How the hell the things are going to work, if there is no SQL? So, Let’s try to understand what does it mean when we say, NoSQL Database. The easiest way to confirm is Yes, there is no SQL if you use these databases. Yet, there is no need to worry too much about it. Perhaps SQL has it’s own limitations, though it has many advantages and ruling the industry for many years. The first and foremost of all is Relationship. For Eg; If you want to store company data like company name, address, departments, employees etc. You could already identify the entities and their relationships like one-one, one-many, many-one etc. If you want to aggregate the data? is there any way? Yes, there are joins, unions etc. So, lets try to fetch the data for the below page and see how many tables will be involved when we have to fetch the data for this page.

Fig: 1

In Figure 1, we have seen the number of relationships you need to just define a structure of a company. If the same thing we have to do with a NoSQL format, how would it look like ? I mean the non relational databases.

This is what is called as Aggregation in NoSQL world. This structure would give you a flexibility of retrieving all the details of the company together and fast, instead of storing the data in different tables and different columns. This difference leads to many other differences between these two worlds.

If you closely observe the above JSON object, each employee object is having different attributes For Eg: HR department has sport, Admin depart has Phone and IT department has non of these attributes. So, this is possible because these databases are scheme less. Each employee and each document might contain completely different format. This could be advantage as well as problem. Usually this is considered as advantages for NoSQL databases because we don’t need to define Schema before hand and this would give the flexibility to add new fields whenever we want in future to the same document.

Obviously this is not all you are looking for, this will provide you a basic idea of connecting a mongo DB using java. The easiest DB connection so far I have seen is this. All you need to do is download the driver class or include it in your pom dependency and create a main class with two method calls.