This is a complete MongoDB introduction tutorial. Here I am targeting audience which is new to MongoDB and knows some concepts of Database, I will compare the concept of RDBMS (like MySql) to MongoDB for easy understanding. So, let’s start this tutorial with what the MongoDB database is-
MongoDB is a document-oriented NoSQL database. Let me go by each word in the above definition-
Collections contain sets of documents and function as the equivalent of relational database tables.
MongoDB offers the following features:
I will explain the above features in this blog on the introduction level, after reading this blog you will get familiar with MongoDB.
Before proceeding deep into the concepts of MongoDB let me tell you where does MongoDB stand in front of all data storage.
If we compare MongoDB with RDBMS and other data storage source in the scale of functionality and Scalability /Performance MongoDB gives almost the same functionality as RDBMS and slightly less Scalability /Performance compare to Key/value storage we can plot MongoDB in a graph as below:
With above graph, you can understand we can leverage the performance of traditional key-value stores while maintaining functionality of traditional RDBMS.
We all use programming languages and deal with objects in most languages, Nowadays, everyone works with objects(Python/Ruby/Java/JS, etc.) OOPS is really a very strong concept in computer science, SQL was invented in the ’70s to store data.
The problem with SQL is that it’s tabular if you want to store anything you need to map your object to a table and need to write the logic for mapping, or if you want to store related data you need to use complex joints so if we need Databases to persist our objects. Then why not store objects directly? MongoDB stores objects (or) documents.
You can relate it with Database in your RDBMS, In RDBMS you need to create DB separably but in MongoDB, it is created on-the-fly when referenced for the first time. The database is made up of Multiple Collections.
The collection is similar to tables in RDBMS, the difference is MYSQL Table is the collection of rows/records with a fixed schema but MongoDB collection is Schema-less and contains Documents. Schema-less means you don’t need to predefined columns as you do in RDBMS here you can have any key-value pair in any record. Collection is indexable by one/more keys and it’s created on-the-fly when referenced for the first time.
A document in MongoDB is similar to a row in RDBMS. You can store JSON objects as a document in Collection. All languages support conversion of the object to JSON and again JSON to native object, each collection in MongoDB can have
_id key – works like Primary keys in MySQL. Document Supported Relationship with linking to another document, I will show you liking in this blog-
MongoDB uses BSON internally to store JSON objects, let’s say you want to store ‘Hello’ as key and World as value, BSON will store it as below :
I hope you have gone through Schema-less is this blog multi-pal times and some of you still want to know why we should use schema-less structure, so in a current way you need to think a log about your DB structure before starting of you coding phase with help of schema-less you can :
The most common use cases for MongoDB include-
So let us talk about some numbers, I have taken stats from MongoDB official site-
MongoDB is not a drop-in replacement for legacy applications built around the relational data model and SQL, based on the use case you can choose from MongoDB or RDBMS, Applications that require complex, multi-row transactions (e.g., a double-entry bookkeeping system) RDBMS will be better to use.
Let say you want to store something in MongoDB for an example an object will look like below:
// find posts that have ‘MongoDB’ tag
Here are a couple of cool MongoDB features-
We, at Habilelabs, use Nodejs, generally as a database of REST API Projects in the MEAN stack. We use mongoose.js object modeling which helps us in writing MongoDB validation, casting, and business logic boilerplates here is the link for mongoose.js: http://mongoosejs.com/
Download this MongoDB Introduction Tutorial PDF Here:
Do you have any queries or discussions on MongoDB introduction drop me a line in the comment box?
I hope you found this post helpful, So don’t forget to share this post with friends.