Developing Applications Using NoSQL Databases

0
9551
image
NoSQL (or ‘Not only SQL’) databases offer the means to store and retrieve data that is not stored in the traditional RDBMS style of tabular columns. NoSQL is rapidly finding a place in Big Data and realtime Web applications.

For the past few years, the world of Web technologies has been associated with assorted programming languages and scripts. The domain of Web development is not confined to a specific programming language or library, using which Web applications and portals are developed. Thousands of toolkits, programming paradigms, scripts, databases and application programming interfaces (APIs) are in use for multiple services and applications. The days when only a static website was needed using classical hypertext markup language (HTML) are long gone. The corporate world is using different tools to design, develop and launch applications with maximum user interactions as well as effective graphical user interfaces (GUI).

SQL, NewSQL and NoSQL databases
Whenever there is need of a Web 2.0 portal (an interactive website), database-oriented applications are required, as the back-end database keeps and maintains the records required for the appropriate functioning of the modules. For example, guest book messages, posts, blogs, e-mail messages, chat messages and comments are stored in the back-end databases so that these can be retrieved, processed or edited at any instant.
Classically, the RDBMS packages used for database applications include MySQL, Oracle, Apache Derby, IBM DB2, IBM Notes, Microsoft SQL Server, PostgreSQL, SQLite, Sybase and many others. These are known as the traditional SQL databases, which are ACID properties compliant.
NewSQL is a contemporary relational database management system, which provides the same scalable performance as NoSQL systems for online transaction processing (OLTP) read-write workloads, and maintains the ACID guarantees of a classical database system.
Nowadays, Web applications use data in heterogeneous formats, which includes audio, video, text, streaming content, signals, images, pixels and many others. In each file, there are a number of file formats. For example, in video, there are a number of file formats including MPEG, MP4, AVI, 3GP, WMV, OGG, FLV and others. In the same manner, image or graphics file formats include GIF, PNG, JPEG, PCX, BMP, TIFF and many others.
Now the major issue is the compatibility of the Web application with all these file formats in different domains. It is at this point that implementing NoSQL databases makes the most sense. In NoSQL databases, any type of file format can be processed and integrated in the Web application.
NoSQL databases provide a storage and retrieval system that is different from the tabular relations used in relational databases. The data structure in NoSQL databases is entirely different from the classical RDBMS. NoSQL databases are rapidly being used in Big Data and realtime Web applications.
There have been various approaches to classifying NoSQL databases, each with different categories and sub-categories. Because of the variety of approaches and the way they overlap, it is difficult to get a clear overview of non-relational databases.

NoSQL table 1

MongoDB is one of the prominent cross-platform document-oriented NoSQL databases released under a combination of the GNU Affero GPL and the Apache Licence. It is free and open source software.

According to media reports, the database technology used for the back-end of the Aadhaar cards in India is MongoDB. Aadhaar implementation involves enrolling thousands of Indians each day and adds terabytes of data to the data repository. MongoDB as well as other data management and analytics software providers continue to produce insights that aid Aadhaar and the lives of millions of India’s citizens.
MongoDB’s customers include companies such as Disney, the New York Times, Cisco, MTV, Forbes, Craigstlist, ADP, AstraZeneca, FourSquare, IBM, Intuit, Microsoft, McAfee, UnderArmour, and eBay.
Initially developed by 10gen (now MongoDB Inc) in October 2007 as a component of a planned platform (as a service product), the organisation shifted to an open source development model in 2009, with 10gen offering commercial support and other services. Since then, MongoDB has been adopted by a number of major websites and services.

Web application development using PHP and NoSQL databases
In order to use MongoDB with PHP, we need the MongoDB PHP driver.
Download the driver from the URL Download PHP Driver.
Now unzip the archive and use php_mongo.dll in your PHP extension directory (“ext” by default) and add the following line to your php.ini file:

extension=php_mongo.dll

Database connection and selecting the database
To make a connection, we need to specify the database’s name. If it doesn’t exist, then MongoDB creates it automatically.

<?php
// connect to mongodb
$m = new MongoClient();
echo “Connection to database successfully”;
// select a database
$db = $m->mydb;
echo “Database mydb selected”;
?>

When the program is executed, it will give the following result:

Connection to database successfully
Database mydb selected

Creating a collection
To create a collection, type:

<?php
// connect to mongodb
$m = new MongoClient();
echo “Connection to database successfully”;
// select a database
$db = $m->mydb;
echo “Database mydb selected”;
$collection = $db->createCollection(“mycol”);
echo “Collection created successfully”;
?>

When the above program is run, it will give the following result:

Connection to database successfully
Database mydb selected
Collection created successfully

Inserting a document
To insert a document into MongoDB, the insert() method is used:

<?php
// connect to mongodb
$m = new MongoClient();
echo “Connection to database successfully”;
// select a database
$db = $m->mydb;
echo “Database mydb selected”;
$collection = $db->mycol;
echo “Collection selected successfully”;
$document = array(
“title” => “MongoDB”,
“description” => “database”,
“likes” => 100,
“url” => “http://www.mynosqldb.com/mongodb/”,
“by”, “My NoSQL Implementation”
);
$collection->insert($document);
echo “Document inserted successfully”;
?>

The above program will give the following result:

Connection to database successfully
Database mydb selected
Collection selected successfully
Document inserted successfully

Finding all documents
To select all documents from the collection, the find() method is used:

<?php
// connect to mongodb
$m = new MongoClient();
echo “Connection to database successfully”;
// select a database
$db = $m->mydb;
echo “Database mydb selected”;
$collection = $db->mycol;
echo “Collection selected successfully”;

$cursor = $collection->find();
// iterate cursor to display title of documents
foreach ($cursor as $document) {
echo $document[“title”] . “\n”;
}
?>

When the above program is executed, it will display the following result:

Connection to database successfully
Database mydb selected
Collection selected successfully
{
“title”: “MongoDB”
}

Updating a document
To update a document, you need to use the update() method:

<?php
// connect to mongodb
$m = new MongoClient();
echo “Connection to database successfully”;
// select a database
$db = $m->mydb;
echo “Database mydb selected”;
$collection = $db->mycol;
echo “Collection selected successfully”;
// now update the document
$collection->update(array(“title”=>”MongoDB”), array(‘$set’=>array(“title”=>”MongoDB Implementation”)));
echo “Document updated successfully”;
// now display the updated document
$cursor = $collection->find();
// iterate cursor to display title of documents
echo “Updated document”;
foreach ($cursor as $document) {
echo $document[“title”] . “\n”;
}
?>

When the above program is executed, it will give the following result:

Connection to database successfully
Database mydb selected
Collection selected successfully
Document updated successfully
Updated document
{
“title”: “MongoDB Implementation”
}

Deleting a document
To delete a document, you need to use the remove() method:

<?php
// connect to mongodb
$m = new MongoClient();
echo “Connection to database successfully”;
// select a database
$db = $m->mydb;
echo “Database mydb selected”;
$collection = $db->mycol;
echo “Collection selected successfully”;

// now remove the document
$collection->remove(array(“title”=>”MongoDB Implementation”),false);
echo “Documents deleted successfully”;

// now display the available documents
$cursor = $collection->find();
// iterate cursor to display title of documents
echo “Updated document”;
foreach ($cursor as $document) {
echo $document[“title”] . “\n”;
}
?>

When the above program is executed, it will produce the following result:

Connection to database successfully
Database mydb selected
Collection selected successfully
Documents deleted successfully
Previous articleWhat is the Haskell I/O?
Next articleJava Virtual Machine (JVM) Delving Deep into its Architecture
The author is the managing director of Magma Research and Consultancy Pvt Ltd, Ambala Cantonment, Haryana. He has 16 years experience in teaching, in industry and in research. He is a projects contributor for the Web-based source code repository SourceForge.net. He is associated with various central, state and deemed universities in India as a research guide and consultant. He is also an author and consultant reviewer/member of advisory panels for various journals, magazines and periodicals. The author can be reached at kumargaurav.in@gmail.com.
The author is an assistant professor in the National Institute of Technical Teachers’ Training and Research at Chandigarh.

LEAVE A REPLY

Please enter your comment!
Please enter your name here