A NoSQL is a popular type of
database system is Mongodb. The installation and working inn general
shall be further.
Installing
Mongodb
I am using
ubuntu 11.10, for which the steps are as follow:-
sudo
apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
For
upstart users-
deb
http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
For
SysV users-
deb
http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen
For
those who are confused what are upstart ans SysV it is nothing but
the different in version of ubuntu/debian. Upstart is an event-based
replacement for the /sbin/init daemon which handles starting of tasks
and services during boot, stopping them during shutdown and
supervising them while the system is running.
The "init"
or "system initialisation" process on Unix and Linux
systems has process ID (PID) "1". That is to say, it is the
first process to start when the system boots (ignoring the
initrd/initramfs). As the quote shows, Upstart is an "init"
replacement for the traditional Unix "System V" "init"
system.
sudo
apt-get update
sudo
apt-get install mongodb-10gen
Now to
work with Mongo
For
starting the mongodb type-
sudo
services mongodb start
For
stopping type-
sudo
services mongodb stop
To
restart-
sudo
services mongodb restart
Now
to use to use Mongodb like SQL we start by typing “mongo” in the
terminal. The assigning and saving values is simple. The commands and
their outputs are shown as below:-
mongo
MongoDB
shell version: 1.8.2
Fri
Aug 10 12:34:22 *** warning: spider monkey build without utf8
support. consider rebuilding with utf8 support
connecting
to: test
>
db.things.save({name:"anjali"});
>
db.things.find();
{
"_id" : ObjectId("5023e84c7c4542be6849ac20"),
"name" : "mongo" }
{
"_id" : ObjectId("5024b2944bed5e5a6dba42e1"),
"name" : "anjali" }
>
Data
can be iteratively added to the db using the command below.
>
for(var i=1;i<=20;i++) db.things.save({x:4,j:i});
>
db.things.find();
{
"_id" : ObjectId("5023e84c7c4542be6849ac20"),
"name" : "mongo" }
{
"_id" : ObjectId("5024b2944bed5e5a6dba42e1"),
"name" : "anjali" }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e2"), "x"
: 4, "j" : 1 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e3"), "x"
: 4, "j" : 2 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e4"), "x"
: 4, "j" : 3 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e5"), "x"
: 4, "j" : 4 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e6"), "x"
: 4, "j" : 5 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e7"), "x"
: 4, "j" : 6 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e8"), "x"
: 4, "j" : 7 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e9"), "x"
: 4, "j" : 8 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42ea"), "x"
: 4, "j" : 9 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42eb"), "x"
: 4, "j" : 10 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42ec"), "x"
: 4, "j" : 11 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42ed"), "x"
: 4, "j" : 12 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42ee"), "x"
: 4, "j" : 13 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42ef"), "x"
: 4, "j" : 14 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f0"), "x"
: 4, "j" : 15 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f1"), "x"
: 4, "j" : 16 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f2"), "x"
: 4, "j" : 17 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f3"), "x"
: 4, "j" : 18 }
has
more
>
The
has more means that the first 20 are displayed in the list. To view
more we have a short command it which displays the rest of the
elements in the list.
>
it
{ "_id"
: ObjectId("5024b93c4bed5e5a6dba42f4"), "x" : 4,
"j" : 19 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f5"), "x"
: 4, "j" : 20 }
>
To
access data from a query, it is slightly different way than in sql.
The find can be done using a while loop as well. Given below:-
>
var icon = db.things.find();
>
while(icon.hasNext()) printjson(icon.next());
{
"_id" : ObjectId("5023e84c7c4542be6849ac20"),
"name" : "mongo" }
{
"_id" : ObjectId("5024b2944bed5e5a6dba42e1"),
"name" : "anjali" }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e2"), "x"
: 4, "j" : 1 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e3"), "x"
: 4, "j" : 2 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e4"), "x"
: 4, "j" : 3 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e5"), "x"
: 4, "j" : 4 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e6"), "x"
: 4, "j" : 5 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e7"), "x"
: 4, "j" : 6 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e8"), "x"
: 4, "j" : 7 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e9"), "x"
: 4, "j" : 8 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42ea"), "x"
: 4, "j" : 9 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42eb"), "x"
: 4, "j" : 10 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42ec"), "x"
: 4, "j" : 11 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42ed"), "x"
: 4, "j" : 12 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42ee"), "x"
: 4, "j" : 13 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42ef"), "x"
: 4, "j" : 14 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f0"), "x"
: 4, "j" : 15 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f1"), "x"
: 4, "j" : 16 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f2"), "x"
: 4, "j" : 17 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f3"), "x"
: 4, "j" : 18 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f4"), "x"
: 4, "j" : 19 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f5"), "x"
: 4, "j" : 20 }
>
We
can use a forEach command just like while loop. In the following
screenshot how it works.
>
db.things.find().forEach(printjson);
{
"_id" : ObjectId("5023e84c7c4542be6849ac20"),
"name" : "mongo" }
{
"_id" : ObjectId("5024b2944bed5e5a6dba42e1"),
"name" : "anjali" }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e2"), "x"
: 4, "j" : 1 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e3"), "x"
: 4, "j" : 2 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e4"), "x"
: 4, "j" : 3 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e5"), "x"
: 4, "j" : 4 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e6"), "x"
: 4, "j" : 5 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e7"), "x"
: 4, "j" : 6 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e8"), "x"
: 4, "j" : 7 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42e9"), "x"
: 4, "j" : 8 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42ea"), "x"
: 4, "j" : 9 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42eb"), "x"
: 4, "j" : 10 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42ec"), "x"
: 4, "j" : 11 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42ed"), "x"
: 4, "j" : 12 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42ee"), "x"
: 4, "j" : 13 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42ef"), "x"
: 4, "j" : 14 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f0"), "x"
: 4, "j" : 15 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f1"), "x"
: 4, "j" : 16 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f2"), "x"
: 4, "j" : 17 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f3"), "x"
: 4, "j" : 18 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f4"), "x"
: 4, "j" : 19 }
{
"_id" : ObjectId("5024b93c4bed5e5a6dba42f5"), "x"
: 4, "j" : 20 }
>
You
are also allowed to treat icon or cursors as arrays by the following
command.
> var cursor = db.things.find();
> printjson(cursor[4]);
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
You can also
convert to an array by using:-
> var arr = db.things.find().toArray();
> arr[5];
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }
Using
as Query
In
sql queries we use sel * from table where condition. and for a
particular case as sel element from table where condition.- The
similar notation in mongo are -
db.things.find(condition).forEach(printjson);
db.things.find(specific condition(s)).forEach(printjson);
findOne()
Lets you retrieve one document via findOne function.
> printjson(db.things.findOne({name:"mongo"}));
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
Limiting the result
This is done using limit function as follows.
> db.things.find().limit(3);
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }
NoSQL and Mongodb are not yet very popular substitutes to SQL but definitely strong scalable comtemporary to RDBMS.