Tuesday, 20 January 2015

Collections and Documents

If you are from a relational background, then you know that data is stored in the form of tables, which has rows and columns .
However in MongoDB, data is stored in the form of collections which is equivalent to tables in MYSQL.
There are no rows and columns as in relational, but data is stored in the form of documents.
The data format of MongoDB is very much inspired from the JSON format which is used in JavaScript, where data is stored in the form of key value pairs.
So to compare,
Tables-Collections
Rows and columns- Documents

Here is a sample of how data is stored in JSON format
{
        "Name" : "Harry"
        "Student_id" : "Bgl14cj001",
        "Type" : "exam",
        "Score" : 54.6
}

The above data inside the curly braces is called a "Document".
Each key-value pair inside a document is separated by a comma(,).
Set of many such Documents is called a "Collection"

However in MongoDB, data is stored in BSON format( Binary JSON) format, i.e., data is stored in binary form inside DB. To know the complete set of Datatypes which BSON supports refer  http://bsonspec.org/

We can also hold array values for a key value pair, like
{
        "_id" : "Ethan",
        "Student_id" : "Bgl14cj001",
        "Type" : "exam",
        "Score" : 54.6,
        "Interests" : ["Chess","Badminton","Painting"]
}

Like in JSON, documents can also hold another document in a key-value pair
For example:

{
"_id" : ObjectId("4f7ee46e08403d063ab0b4f9"),
"name" : "MongoDB",
"notes" : [
    {
        "title" : "Hello MongoDB",
        "content" : "Hello MongoDB"
    },
    {
        "title" : "ReplicaSet MongoDB",
        "content" : "ReplicaSet MongoDB"
    }
]
}


Here, you can see that "notes" hold 2 documents as an array, an herein lies the true power of Mongodb.
We can hold any number of data inside a single document  and each data can be nested.

The key difference between structured and Unstructured is as i have explained before, the Schema-less nature.
That means each document can have its own structure like

{
        "_id" : ObjectId("4f7ee46e08403d063ab0b4f9"),
        "Name" : "Harry",
        "Type" : "exam",
        "Score" : 54.6,
        "Interests" : ["Chess","Badminton","Painting"]
}
{
        "_id" : ObjectId("4f7ee46e08403d063ab0b4d3"),
        "Name" : "Jenny",
        "pincode" : 500231
}

and This is valid!!

If u have observed carefully, each document has a field called "_id", which is holding either a hexadecimal format ObjectId or a predefined value such as "Ethan".

This _id field is more like the primary key in a relational database which uniquely identifies a particular document in the db.

More on _id field:
This _id field is automatically generated by Mongodb when creating a document.However, we can override it by our key by assigning a value while creating a document. I will discuss this afterwards on the creating documents section.

_id is a 12 byte hexadecimal value.In This first 4 bytes are for the current time stamp, next 3 bytes are for machine id, next 2 bytes for process id of Mongodb server and remaining 3 bytes are simple incremental value

Basic mongodb commands



No comments:

Post a Comment