Thursday, July 17, 2014

Hands on MongoDB for Ubuntu 12.04 LTS

While I was working on TikiriDB database abstraction for wireless sensor networks (WSN), a big argument we had is about SQL and NoSQL. Even though the argument is about database abstractions for WSN, not exactly about database management systems for conventional information systems, it was the first time I heard about this new concept, NoSQL. Since then, I have been working on many projects but didn't get a chance to work closely with any NoSQL database system. After a long time, that long awaited moment arrived.
As a very popular NoSQL database system, MongoDB is something which I could not avoid. While working on some project, I came across it and had to install and work with it in my Ubuntu platform. To to leave a note about it, I decided to write about how I installed it and moved around the database system to explore. Since my platform is Ubuntu 12.04, the installation process is pretty straightforward. Everything can be accomplished from the command line. So, issue the following commands to install MongoDB.
sudo apt-key adv --keyserver hkp:// --recv 7F0CEB10

echo 'deb dist 10gen' |  sudo tee /etc/apt/sources.list.d/mongodb.list

sudo apt-get update

sudo apt-get install mongodb-org
At the end we can start the MongoDB server from the following command.
sudo service mongod start

 Now, we can play with the database using a command line client. By default with the database installation we are getting a client called mongo. Start it by typing that command name on the terminal and you should face the query prompt of the mongo client. We can list the current databases by issuing 'show dbs' command on the terminal. Among the output, there should be a database called 'local' by default. Lets move into it by issuing the command 'use local' and after that typing 'db' will confirm that we are in the 'local'.

Inside a MongoDB database such as this 'local' database, there are items called 'Collections' which resemble 'Tables' in a SQL database. Now, we can check what are the collections in this 'local' database by issuing the command 'show collections'. In my system, the only two collections in this database are 'startup_log' and 'system.indexes'. If we want to see the data in one of these collections, we can use find() function in the following way.


It should list down the contents in this collection which are lot of configuration information of the MongoDB database installation on my platform. This is all for now. More things about MongoDB can be learned from the official MongoDB documentation.

No comments:

Post a Comment