Showing posts with label BSON. mongoDB. Show all posts
Showing posts with label BSON. mongoDB. Show all posts

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

Thursday, July 14, 2011

Understanding BSON

BSON stands for Binary JSON. The difference between JSON and BSON is that BSON is like binary representation of JSON document or more like serialization i.e to convert JSON document to serialized document with some extra characters. This way BSON supports data structure types that are not available in JSON, like BinData type.

BSON is lightweight, traversable (used in MongoDB), efficient.

BSON in many cases has larger size than JSON document so why should we use it?

Well it the main goal for this is the traversability i.e faster processing. When creating BSON document in the code some extra data is added like length prefixes so that it is much more easier to decode it. Also integer numbers are stored as 32 bit integer so that there is no need of processing text . Thats why it is much faster but it takes more place.

You might wanna visit BSON . Also visit the specifications page. 

In the next post I will talk about implementing the MongoDB and installing php drivers.