MongoDB : Basics

MongoDB is a document oriented database.  Classified as a NoSQL database, MongoDB removes the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls the format BSON- Binary JSON).

JsonDocument

This notation is more close to what we have inside our programs. The same is implemented in RDBMS in different tables with foreign keys. Lets install and see first. Download the installable from its official site. I am using windows 64 bit machine and downloaded the msi installable. Note that if you are not the admin user , some error might come while installation, for that you can downloded the zip version from ‘Build Archive’ link. Lets see the files in the bin folder. These will be same whatever your mode of installation may be:

binFiles

mongod.exe – Actual DB service
mongo.exe – Its actual a javascript console, i.e we would be usinf JS to interact with mongodb
mongoimport/mongoexport – Import/Export DB as JSON
monogodump – To take backup in binary format. This gives the BSON files.
mongorestore – To restore from BSON files.
bsondump – Convert Bson to Json.
monogostat – overview of status of the currently running mongo instance.

Before we start the services , it would be good if we know some of the NOSQL DB terms. Please note that these terms are not exactly 1-to-1 comparison from SQL.

  • Database – Same as SQL, where you have all your tables etc
  • Collection – Similar to table in RDBMS but unlike a table a collection has no schema. You may enforse a schema from your programming code but actual db itself doest not enforse any schema. You can have documents having different fields in the same collections, although its not a good practice but its technically possible.
  • Document – Like a row in RDBMS.
  • Field – Like a RDBMS column having a key:value pair.

To startup we will first run mongod.exe. You will get an error something like this:

mongodError

This is because dbpath is not set. By default mongodb needs a directory to store all its files and by default its \data\db. You can make this path or use –dbpath everytime you run mongod. Better to make a script for it. I have created the default directory in my c drive.

Now to connect to mongoDB we will be using the mongo shell. Start it by running mongo.exe

mongoshell1

As shown above, we used show dbs to check see how many databases are there currently. To connect to a database manually we used ‘use <dbname>‘. Notice that we didnt create any DB just used and if its not there mongo will create it. Also notice that mongo doesnt create any db or collection until we store some document to it.

Lets insert some documents. We will use a db called company and have a collection of employees

mongoshell2

As seen above, we used insert method to pass jso object to insert in the collection of employees. Since mongo shell is a js console, we can insert like following also:

mongoshell3

Above we used a variable and then added properties to it. Notice that we have use the save method. Its a higher level method. It checks if the object we are inserting has an _id field, which is the primary key in mongodb document. If _id is mongo will call update methd, else it will call insert method. To see what are doc are present in an collection we use find method


db.employees.find()

db.employees.find().forEach(printjson)

mongoshell4

Find method will give all the raw data while the foeEach will format in pretty json format.

Now that we have a basic understanding of mongodb, lets see some of its difference with typical RDBMS. RDBMS is not known to be scalable easily. You can scale up pretty well by adding more harware like RAM,space etc but on a single node. Mongo can scale horizontally very easily. There are 2 main difference with RDBMS:

  1. MongoDB does not support JOINS – Each document is stores in a collection, but you cannot do a join of 1 document in one collection with another document in another collection. The reason being joins wont work good when you scale out to different nodes.
  2. MongoDB does not support TRANSACTIONS – This may sound bad but you wont need transaction in mongoDB apps, because documents are hierarchical and you can update this document in one go atomically. Hence there is no need for multiple update of table like rdbms.

NOSQL dbs should not be thought as a replacement of SQL dbs. They are differnet technologies to do different things. NOSQL dbs are good when you work with huge amounts of data. Its designed for cluster of servers.

Advertisements
%d bloggers like this: