Saturday, July 16, 2011

Installing MongoDB

I use MongoDB on Ubuntu but you can also use it on Windows. I will explain how to install it on Ubuntu.

I installed myselft with oppening terminal and typing:

$ sudo apt-get install mongodb

Wait a moment and it will be installed. There is an option to build it from binary sources but this is much more complex (In other post).

Next, you must create directories for the db so open terminal and type in these commands:

$ sudo mkdir -p /data/db/
$ sudo chown `id -u` /data/db 
 
This creates folder data/db at root level and change owner to root. 
Now you should be able to use MongoDB. Start the server by typing in terminal:
 
$ sudo start mongodb  or this $ service mongodb start. You will get a message 
whether the service is started. If it is started than go to this folder /bin and type  
mongo in terminal i.e
 
$ cd /bin
$ mongo    you will get the ">" char in terminal so it means it works.
For start type in 
 
> use mydb 
> db.testcol.save({json : "this is a json object"});
> db.testcol.find();
You should get something like this as output:
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "json" : "this is a json object" }
 
With these commands we use the database called mydb and than create a collection 
(table) called testcol and save one BSON document (row)  
{json : "this is a json object"}. The find command prints all document in the collection  
testcol. You should visit the SQL to Mongo Mapping page at SQL-Mongo
 
You can notice that we use json notation to create documents (rows) in the database, 
Mongo works its way i.e it constantly converts from json to bson and vice verse. 
 
I will add some examples on how to work with Mongo so make sure you read well 
about json.  
 
 You might wanna visit the downloads page @ downloads
Select you operating system version and download it.  Also visit Quickstart

No comments:

Post a Comment