An Introduction to MongoDB

What is MongoDb?


MongoDb is a opensource database, that stores data in a document-oriented data model. It is what an example of what is commonly known as a NoSQL database. But let's get a bit thorough on this. 

Normally, we are usually introduced to Relational Databases, like MS Access. Those are primarily SQL based languages. They have tables in which data is stored. Each table has multiple rows, and each row has multiple columns or fields. The data stored follows a well defined schema. Moreover, relations can be set-up between various tables, so that redundant data is not stored. One of the main advantages is that we can thus change the data in only one location and the effects are observed everywhere. Also, due to the presence of a schema, we know beforehand what all fields exist in the tables. But enough about Relational/SQL databases. 

MongoDB, however is very different from the setup. Instead of tables, it has what are called as "Collections", though one can be roughly seen as the equivalent of the other, though as we shall see, they are not the same. Anyway, moving on, each Collection has a set of Documents(written in JSON) , which again, can be thought of as the row-equivalent. However, the key difference here is that there is no schema for the row, or as such there is no schema at all. This means that each row can store as much information it wants. There are no set number of fields. Thus, the principle is that instead of establishing relationships across various tables like we do in a relational database, we instead dump the whole related information in a single document, as no schema is enforced from beforehand and thus data can be added in a document as per need. 

Let's clarify this a bit more. For example, in a relational database, if we want to store patient information, we would have to create a primary table where there would be a name and user id. Then we would need to have additional tables to store multiple(possibly) phone numbers, to store addresses, to store multiple prescriptions etc respectively. Thus, it can quickly get out of hand and it becomes increasingly cumbersome to add more tables to store these informations. Of course we cannot store them in a single table because that would mean a sparsely filled table(of course, not all patients would have multiple phone number or addresses, but we would need to keep field for them incase it was needed) which would ofcourse be very inefficient.

In MongoDb however, we can have a Collection name PatientRecords, where we can have multiple Documents specifying each patient record. Each such Document can have varying amounts of information about that particular patient. However the main difference in this model is that all the information regarding a single patient is stored in only one "document" in contrast to the multiple tables required to store the same information in a SQL database.  Moreover, if we felt like it, we can add even new information to whichever Document that we feel like without breaking preexisting code. 


Some Advantages

  • First, we can add and remove as much data as we want, to each Document separately. Even though this means that we are not guaranteed that a certain field will exist, it nonetheless is a much more flexible approach which lets us add more data very easily.
  • The way it stores information in documents is very effiecent as it can easily store structure information in JSON format like arrays and objects.
  • Read queries are much faster than in conventional databases as we dont have to look across multiple tables -- all the information is collated in one place. 
  • It is very easily scalable. Other databases do not allow much scalability as we cannot really add servers and distribute the tables across these servers, and even if we could, it would be very difficuly. Horizontal scaling was thus a difficulty. However, a key goal of mongoDB is to make scaling easy. It supports horizontal scaling effectively via sharding.

Application

There is no winner between mongoDB and the more classical DBs that use SQL, its just sometimes MongoDB is useful, sometimes SQL based DBs are. Nonetheless, following are some of the places where mongoDb and its scemaless document oriented data storage model is especially useful.

  • The first point to note is that mongoDB should be used where there are more read operations than write, as write is pretty redundant because we have to change all instances of that data. Read operations are however much faster than in other DBs.
  • When scaling is going to be a factor. This is because mongoDB is a very easily scalable platform.
  • When the design is loosely modelled and may change over time.
  • Evolving Data requirements.

Setting up the mongoDb Environment

To setup the environment for mongoDb, follow these steps : 
  • First, go to this website, locate the download button (this website keeps changing, but hopefully its still at the top left :P ) and download from the server/community server section for your respective OS. This is going to be primarily based on osX, but its similar for windows too.
  • After the download, extract all the files in a suitable directory.
  • Now, mongoDb usually stores its data in a /data/db folder which we need to create within the location in which mongoDb was shifter.
  • Next we need to manually set the permission for this "db" folder. For that, use the following command : sudo chown <your_mac_username> <relative_location_of_folder>.   In our case, /data/db should be the location of the folder.
  • Also, make sure that the binary files are there in the Path for your environment, which are initially present in the bin folder of your extracted folder. Modify the shell's path to include this directory.
  • And we are done. we can now run the mongod command directly.

Some Basic Operations in MongoDB

I will be going over some of the basic operations in mongoDB

  • Creating a new Collection -- Creating a new collection is supereasy -- all we have to do is execute : db.createCollection("CollectionName")
  • Inserting Documents -- We can insert new documents very easily using commands like db.collectionName.insertOne({JSON formatted object with key:value pairs}). We can also insert multiple documents using the insertMany command which takes an array of JSON objects as parameter.
  • Reading and finding Documents -- We can find our required document using the db.collectionName.find() command which takes a {key:value} pair as parameter for searching all documents satisfying the filter. We can also add more complex filters, but that would be the material for a much more thorough discussion on this topic.
  • Updating Documents -- There are again several ways to update a document using commands like db.collectionName.updateOne() etc. In the normal version however, we need to rewrite the whole document, which seems a bit redunant. Thus we can use several keywords like set which basically appends the new information to the end of the document. Another concept is that of an upsert which basically means that in case there was no document matching our search parameters, it would create a new document, and thus would rather act as in insert.
  • Delete documents -- As with the previous commands, delete also follows a similar structure where we can delete one or more documents using commands like db.collectionName.deleteOne() and deleteMany(), with the appropriate filters.

Hopefully this was a swift introduction to the world of mongoDb. This is just meant to be an overview, as obviously, mongoDb is a vast topic. I have just tried to give a gist of the main features of mongodb.




















Comments

  1. Thanks for sharing this wonderful content.its very useful to us.I gained many unknown information, the way you have clearly explained is really fantastic.keep posting such useful information.

    oracle training in chennai

    oracle training institute in chennai

    oracle training in bangalore

    oracle training in hyderabad

    oracle training

    hadoop training in chennai

    hadoop training in bangalore

    ReplyDelete
  2. This was an authentic and useful piece of information. Thank you for giving this useful content.
    Ethical Hacking Techniques
    What Is a Hacker

    ReplyDelete

Post a Comment

Popular posts from this blog

Small-to-Large

Segment Tree Beats - An introduction

Getting prepared for RMO