Example of MongoDB

In Previous tutorial we have discussed about the Features of MongoDB. In this tutorial, we will understand MongoDB practically with an example.

Server-side JavaScript execution:

JavaScript can be used in queries, aggregation functions (such as Map Reduce), are sent directly to the database to be executed. The reason is that the document is stored in the database in the BSON format that is Binary JSON format (Java Script Object Notation) which supports Java Script functions.

Example of Mongo DB:

Unlike other Relational database they are used schema of table, column, data types. Here is in Mongo DB it is basically n*n collection which is of type JSON or BSON. Suppose we want to basically make a table in oracle, that is student table, we have 2 attributes named as name and age.  So if a user wants to insert data in the table then we have to specify name and age. But in Mongo DB it needs not to define how much column this table will contain, and, in this column,  it could be a n*n that is n number of columns. There are works on basically JSON principle.

JSON is a key value pair in which number of pairs can be dynamic. Suppose the attribute which are inserting into the table the attributes can be A, B, C, D. So there can be n number of column and each column can be itself be a Map again. A can contain other attribute let x, y, z, so we can say it is basically a map in which each entry within it itself be a map.

Let’s observe an example.

Suppose I am inserting a document (row).

db.collection.insert ({“author”:{“name”:”Pratik”}})
db.collection.insert ({“author”:{“name”:”pratik”,”age”:21}})
db.collection.find ({“author.age”:21})

In the above sample example:

  • collection = Collection
  • db = Database
  • author = Column name
  • name = attribute or column name of author
Example of Mongo DB:

At the run time we can add the attributes in the column. That means number of attributes which we are defining at run time are dynamic. This features couldn’t possible in case of other relational databases.

Comparison with other relational database :

MySQLMongo DB
DatabaseDatabase
TableCollection
Tuple (Row)     Document
ColumnField
IndexIndex
Primary key    _id Field
JoinEmbedded Document
PartitionShard
AggregationAggregation Framework

Some of the clients of Mongo DB:

There are lots of clients are in Industry. Some of them are:

  • MTV networks
  • SAP AG(popular ERP company)
  • Craig List
  • MTV
  • Foursquare
  • Source forge

Summary

  • JavaScript can be used in queries, aggregation functions (such as Map Reduce), are sent directly to the database to be executed.
  • Unlike other Relational database they are used schema of table, column, data types. Here is in Mongo DB it is basically n*n collection which is of type JSON or BSON. Suppose we want to basically make a table in oracle, that is student table, we have 2 attributes named as name and age. 
  • At the run time we can add the attributes in the column in MongoDB.
  • JSON is a key value pair in which number of pairs can be dynamic

In this tutorial, we have understood the MongoDB with an Example. In next section, we will discuss about the Components of MongoDB.

Leave a Reply

Your email address will not be published. Required fields are marked *