In order to implement different machine learning algorithms and AI techniques to predict and solve problems, pre-existing data is required to be used in the many calculations involved. Data storage has become rather important as the size of an average company data set is increasing. In order to organize all of these raw data into an easily readable format where data can be easily read, inserted, updated and deleted. There are several database software that accommodate these features along with their own unique signature such as follows :
![]() |
| Microsoft Access |
![]() |
| Microsoft Azure |
![]() |
| MySQL |
I'd like to talk about a specific database management software called MongoDB; Its strong points and how to start with it as a complete beginner.
MongoDB is a JavaScript, C and C++ written document oriented database program and is widely used by many web applications as its data storage management software.
Strong Points of MongoDB
There are many advantages of using MongoDB in your web application:
- MongoDB is free, open-source and cross platform supportive.
- MongoDB is known to have Horizontal Scalability; instead of getting better memory and processing power for one specific machine, the work load is split between multiple machines with equal memory capacity, therefore the same database can be spread across many different servers.
- MongoDB has a replication mechanism that prevent data loss when one of these many servers happens to go down.
MongoDB uses a NoSQL concept, where there is no object oriented concepts in its stored databases such as normalization. It's counterpart SQL forces the user to use a structure, which is not flexible when it comes to scalability. In addition, NoSQL gives developer startups such as college students, a way to store data without worrying about data structure and has the freedom to set a data structure later on. NoSQL has a much higher performance than SQL, and is specialized to retrieve data using queries. The biggest visible difference between SQL and NoSQL is that, SQL has a table for every entity whereas NoSQL contains no such things and only follows its principles through Documents and Collections. Through the eyes of an SQL developer, this would seem like creating one large table to store EVERYTHING required within a system.
How to get started
If this is your first time with MongoDB, the following steps will ensure you will be able to enter and manage your way in the software without any worries:
-----------------------------------------------------------------------------
Download and Install MongoDB
MongoDB has to be downloaded from its website and installed into your computer. As MongoDB is cross platform supportive, there will be different versions available for different Operating Systems (OS).
Click here ->Download MongoDB
Once the executable file has been downloaded, run it to initialize install. Say Yes for everything and make sure you confirm the location of the install directory of the files (Default is C:\Program Files\MongoDB)
-----------------------------------------------------------------------------
Download a sample data file
Firstly, go to the following provided website to download sample data about bank customers. The downloaded files contains a JSON file called bank_data.json. This JSON file contains 50,000 records of bank customers which include details such as First Name and their Account Type.
Click here ->Download Sample Data
| Click to Zoom In |
If you have successfully downloaded the JSON file and open it up on Notepad, it will look like the above picture. Basically, an entire mess of 50,000 documents of bank customers, most with multiple customers. Whats left for us to do, is to organize the shown data into a database and within the database, a collection. A collection is a grouping of MongoDB documents and does not force a schema onto the user, like an SQL table would.
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
Start a MongoDB Server
You will now need to start up a server first in order to host the database you need.
First of all, find a directory that you want to store your database files in. For this example I chose C:\CodeQuest\cq_data.
Then open up a command prompt window and change your current directory to the directory in which you've installed the MongoDB bin folder (if you have already entered the \MongoDB\bin into the PATH environment variable then you can skip this line):
cd C:\Program Files\MongoDB\bin
Next, set up the server using mongod and direct at to your chosen directory. Earlier, I chose C:\CodeQuest\cq_data and so will be typing the following:
mongod --dbpath C:\CodeQuest\cq_data
Upon success, the following response will be shown on the same command prompt window:
| Click to Zoom In |
-----------------------------------------------------------------------------
Start a MongoDB Client
Now that the server has been created and is on standby, it's time to create a MongoDB client on the same laptop.
Open up another command prompt window and change the directory to the MongoDB bin directory again(if you have already entered the \MongoDB\bin into the PATH environment variable then you can skip this line):
cd C:\Program Files\MongoDB\bin
Its time to import the sample data that you have downloaded, into the MongoDB database (which is currently running on your server). As said before, the sample data file is in JSON format and will have to be imported as a JSON Array. Type the following code into your command prompt client window :
After --db enter the name of the database you want to create and import into (bank), after --collection type the name of the collection you want to store all the documents from the JSON file (customers), and after that type the location of the downloaded bank_data.json. If still confused then please refer the following response to what I have entered :
It will connect to the localhost and attempt to import all the documents into the collection that I have selected (customers). As said before, the sample data contains 50,000 documents and as it states that 50,000 documents have indeed been imported, we can assume that it was a successful import.
Time to finally start the MongoDB client! Within the same command prompt window, simply type :
and the below outcome will occur:
mongoimport --jsonArray --db bank --collection customers c:\location\of \file\bank_data.json
After --db enter the name of the database you want to create and import into (bank), after --collection type the name of the collection you want to store all the documents from the JSON file (customers), and after that type the location of the downloaded bank_data.json. If still confused then please refer the following response to what I have entered :
| Click to Zoom In |
Time to finally start the MongoDB client! Within the same command prompt window, simply type :
mongo
and the below outcome will occur:
| Click to Zoom In |
-----------------------------------------------------------------------------
Initializing the database
Now, lets try to access our database and collection that we have imported our data into. Type the following the command into the mongo client console:
show dbs
This will show the current databases you have created thus far, including the default admin database. Since we have created and imported data into a database called 'bank', let's remind MongoDB that we want to use it by typing the following command:
use bank
The following response will be shown to confirm that the 'bank' database is now initialized:
| Click to Zoom In |
show collections
-----------------------------------------------------------------------------
View and Query
This is an important feature that all databases require to allow the users to extract and view stored data. In order to view the right kind of data, they will have to first be queried by utilizing custom criteria and the 'find' command statement.
Now that the bank database's customer collection is containing actual documents, we can now retrieve these stored documents by using the 'find' command in MongoDB.
Finding the 1st Record
Let's start with something simple. We can find the first identified document in our collection by typing the following command :
db.customers.findOne()
Where 'customers' is the name of the collection that we are using the query on and 'findOne()' is the method that we are calling to the document and record number 1. Such a result will be shown :
| Click to Zoom In |
This is the structure of the bank data procured from the customers collection. Above the first_name field of the shown document is an "_id" field that contains the Object ID of that specific document.
Finding all the JAMES
Let's say you have thing for guys with the first name "JAMES" *wink wink* and you want a list of all the customers in the criteria. All you have to do is to call the find() function and pass your parameters:
Finding all the JAMES
Let's say you have thing for guys with the first name "JAMES" *wink wink* and you want a list of all the customers in the criteria. All you have to do is to call the find() function and pass your parameters:
db.customers.find({first_name:"JAMES"}).pretty()
and voila! The following result is shown on your command prompt:
The result is as follows:
similarly, if you want to find the 12th record in the list, just type:
This is the desired result :
-----------------------------------------------------------------------------
and so it will be shown as follows:
| Click to Zoom In |
...good luck trying to figure that out. These are all data related to every single customer with the first name "JAMES" in the bank database, but its all in a very unorganized format. We can fix that :
db.customers.find({first_name:"JAMES"}).pretty()
| Click to Zoom In |
Now, everything looks so much better :) . Notice how you can view all the different kinds of data of each and every "JAMES" and the time it took to retrieve all that information from the database. Impressive right?
But if you want only certain pieces of information without being distracted by unnecessary segments in every record, all you need to do is to further filter your find() function. The following code statement only retrieves the first name and last name of every record:
db.customers.find({first_name:"JAMES"},{first_name:1,last_name:1}).pretty()
The result is as follows:
| Click to Zoom In |
similarly, if you want to find the 12th record in the list, just type:
db.customers.find({first_name:"JAMES"},{first_name:1,last_name:1}).pretty()[12]
This is the desired result :
| Click To Zoom In |
Coding in JavaScript
As MongoDB is operated with JavaScript, you can code in JavaScript within the console to retrieve more specific results. One such example is finding all people who have a last name of "WOLF" and printing all their full names one by one:
var wolves=db.customers.find({last_name:"WOLF"},{first_name:1,last_name:1,"_id":0});
We will first create the query and store the results in a variable called wolves.
Then we will run a FOR loop to print out every element in the list:
for(var i=0;i<wolves.count();i++){
print(wolves[i].first_name+' '+wolves[i].last_name);
}
and so it will be shown as follows:
| Click To Zoom In |
Well this has been a rather long tutorial to start off with. However, as you play around with MongoDB you will soon realize the potential of such an alternative to traditional SQL Databases. I myself am currently using MongoDB for my own pursuits in Machine Learning and will be continuing to release even more content to help you guys out! :)





Comments
Post a Comment