A tutorial and explanation about the basics of MongoDB

MongoDB is an opensource NoSQL database implementation, founded by the people behind DoubleClick, ShopWiki and Gilt Groupe. NoSQL is a different approach compared to traditional RMDBS databases. Traditional RMDBS databases (for example Oracle databases and MS SQL databases) are based on structured data. In a typical RMDBS environment, we first analyse how data could be saved in a structured way. Based on that analysis, we create tables and queries to store and retreive data from the database. The power of NoSQL databases is that it does not use a predefined relational data model. NoSQL databases stores data in a more generic way, based on keys with corresponding values.

MongoDB is a NoSQL database implementation based on JSON documents, which stands for Javascript Object Notation. JSON is an open standard to define data in a ‘human readably’ way, also also known as the succesor of XML. JSON is often used in RESTful services, NodeJS and Javascript applications.

I made a basic setup how MongoDB could be used. In my example I create a MongoDB environment and interact with MongoDB. In my case I use Java to interact with MongoDB, but other languages, such as C, C++, C# or Javascript, is also supported.

Now let’s setup our MongoDB environment. In my case, I use a Windows machine but you could also use Linux or Mac OSX. First download and install MongoDB from https://www.mongodb.org/ on your machine. In my case, I installed MongoDB on the C drive. Before you start, it is important to create the folder structure C:\data\db. MongoDB uses this folder to save the data of your databases. Without this folder, MongoDB does not start. After you created the folder and installed MongoDB, start the command prompt and run mongod.exe:

Congratulations, you got your first MongoDB server running! The server is running on localhost and listening on port 27017. Next step is to download the driver. The driver is language specific and is needed to connect and interact with the MongoDB server. You can find MongoDB drivers at https://docs.mongodb.org/ecosystem/drivers/ . In this example, I will use Java so I downloaded the Java driver and added the driver in my IDE. In my example I use JDeveloper but any IDE is fine to use.

In the code below, I create a connection to my MongoDB server, get my database and fill the person collection with some data.

//Create a connection
MongoClient mongoClient = new MongoClient("localhost", 27017);
        
//Get the database I want to work with (when the database does not exists, MongoDB will create one)
MongoDatabase db = mongoClient.getDatabase("MyTestDB");
        
//Get my collection of 'Persons', comparable with a table in RMDBS databases
MongoCollection<Document> persons = db.getCollection("Persons");
        
//Add first person
Document person1 = new Document();
person1.put("name", "PietjePuk");
person1.put("age", 12);
persons.insertOne(person1);
        
//Add second person
Document person2 = new Document();
person2.put("name", "Ewout");
person2.put("intelligence", 9000);
persons.insertOne(person2);
        
//Add third person
Document person3 = new Document();
person3.put("name", "Joep");
person3.put("age", "veryHigh");
persons.insertOne(person3);

After the database is created and data is inserted, you can also see MyTestDB is created in the D:\data\db folder. These files contain the data:

One of the big differences you may noticed between MongoDB and RMDBS is that I don’t need to do any prework on the database. You don’t need to create a database, nor a person collection. The reason for this is that MongoDB only knows the type Document and everything is based on the type Document. A Document only contains a set of keys with a set of values (in my case name, age and intelligence). This is very handy for unstructed data, because it is based on keys and values. I don’t need to analyse data. Like I described before, this is one of the advantages of using NoSQL databases like MongoDB.

After the database is filled with some data, I also want to print the inserted data from the MyTestDB. This could be achieved by using the following code:

//Create a connection
MongoClient mongoClient = new MongoClient("localhost", 27017);
        
//Get the database I want to work with (when the database does not exists, MongoDB will create one)
MongoDatabase db = mongoClient.getDatabase("MyTestDB");
        
//Get all the documents (persons) in the iterator        
MongoCollection<Document> persons = db.getCollection("Persons");
FindIterable<Document> cursor = persons.find();

//Iterate over all documents and print
for(Document document : cursor) {
      System.out.println(document);
}

Output:

MongoDB also supports filters for querying. For example, I only want persons with the name ‘Ewout’

FindIterable<Document> cursor = persons.find(eq("name", "Ewout"));

for(Document document : cursor) {
      System.out.println(document);
}

Output:

In this article, I’ve explained the basics about MongoDB and also how to setup a MongoDB environment. MongoDB is an excellent choice for Big Data solutions, because it could save unstructured data, it is easy to scale horizontally and has an excellent performance. MongoDB is also an excellent solution for caching. Will NoSQL databases terminate the traditional RMDBS? Time will learn. But with the growing amount of unstructured data, NoSQL databases will help us a lot!