Mongo DB

Mongo DB Basics

Jun 17, 2020

 

What is MongoDB?

Mongo-DB is a cross platform document oriented NoSQL database. It make use of JSON like documents with optional schema's.

All the data is stored as an document in MongoDB which is one of the type on No SQL Databases. It is used to store a very big amount of data.

MongoDB was originally started to build by a company by name 10gen in year 2007, later in year 2013 they changed their name to MongoDB Inc.

Understanding the Semantics of Mongo DB.

MongoDB does have a concept of database, so all the data related to a single project will be stored inside a database, but instead of having tables made up of rows and columns , in MongoDB we have the concept of Collections and documents.

Unlike an RDBMS Table, here a document is made up of key value pair which is not required to have a similar columns for all the records, for instance let say you are going to create a website where any one can fill up their profile based upon their professional and personal choice, think of it like instead of giving them a structured form you are giving them a blank paper and asking to fill up whatever they want, but now you want to manage the data under a single table, which is with SQL feasible but with lot of juggernauts. To handle this solution, MongoDB provides you with an option of documents instead of combination of key and value pairs.

So here documents under a single collection can have different key value pairs and it is not language dependent so any one can actually use them.

For example consider the following document

{
"orderID":"12345",
"customerId":"C-009",
"orderDate":"2020-05-01";
"deliveryDate":"2020-05-04",
"Cart":[
    {"productId":"45",
     "qty":"5",   
     "pricePerUnit":"340"
    },
  {"productId":"98",
     "qty":"1",   
     "pricePerUnit":"1209"
  }
],
"address":{
"sector":"13",
"plot":"43",
"building":"ABC Complex",
"landmark":"Near  Bus Stop"
},
"_id" : ObjectId("5ee7806d69e77f3ba8e2fd15")

}

Now if you consider the document above , it contains all the details of an order without making any joins of multiple table. For instance if the same table was in SQL , it would have been required to create a table by name orders and cartItems and then use the orderId as a primary key and then link it in cartItems as foreign key for the references and create a one to many relationship here. So rather than creating it we have actually embedded all the data about an order inside a single document. Easy to Manage. Easy to Code.

Now if you observe the last key value pair in the above example you can see a key with name "_id" which points to a ObjectID(alphanumeric), is actually generated by the MongoDB as a unique identifier for the document, in ideal situation we should always use the find operations on MongoDB data using _id.

Related Posts

Table Of Contents

;