What is “No” in NoSQL ?

What is “No” in NoSQL ?

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.

SQL-Relationships
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.


{
'Company':'XYZ Company',
'Departments':[
'Dept1':
{ 'Name': 'HR',
'Employees':[ {'Name':'A','address':'City A', 'Sport': 'Tennis' },{'Name':'B','address':'City B', 'Sport': 'Cricket' } ]},
'Dept2':
{ 'Name': 'Admin',
'Employees':[ {'Name':'X','address':'City X', 'Phone':12121 },{'Name':'Z','address':'City Z', 'Phone':232323 } ]},
'Dept3':
{ 'Name': 'IT',
'Employees':[ {'Name':'N','address':'City N' },{'Name':'M','address':'City M' } ]}
],
'Addressg':'#55, 10 Avenue, ABC City, US'
}

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.

References:

The Book – No SQL Distilled by Pramod and Martin Fowler would give you a very good understanding of NoSQL.

Blogs you would like to follow – nosql.mypopescu , NoSQL Weekly

Happy Reading 🙂

How to connect MongoDB using Java

How to connect MongoDB using Java

Here are the steps to connect the MongoDB from the java class.

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.

You are ready to go !!!

Step 1: Download / Add your pom dependency.

pom.xml looks like this


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.apc</groupId>
 <artifactId>m101j</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <dependencies>
 <dependency>
 <groupId>org.mongodb</groupId>
 <artifactId>mongo-java-driver</artifactId>
 <version>2.10.1</version>
 </dependency>
 </dependencies>
</project>

Step2: Java class looks like this


package com.abc.mongo;

import java.net.UnknownHostException;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class Main {

/**
* @param args
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException {

MongoClient client = new MongoClient( new ServerAddress(&quot;localhost&quot;, 27017));

DB db = client.getDB(&quot;DBNAME&quot;); \\ Usally there is always a DB with name test

DBCollection collection = db.getCollection(&quot;COLLECTIONNAME&quot;); \\ You can easily create a collection by going to Mongo Shell.

DBObject document = collection.findOne();

System.out.println(document);
}

}

Try to run this main program and you would be able to see the output of the Collection results on the console.

Hope this was helpful. Thanks

Other Useful references :

MongoDB Commands Help

MongoDB University Where you can attend MooC