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 is MongoDB database?
What is MongoDB Database?
MongoDB is a document-oriented NoSQL database.
Let me go by each word in above definition
(1) NoSQL: Instead of using tables and rows as in relational databases, NoSQL is built on an architecture of collections and documents.
(2) Document-Oriented: Documents comprise sets of key-value pairs and are the basic unit of data in MongoDB.
Collections contain sets of documents and function as the equivalent of relational database tables.
MongoDB Provides following features :
– Scalable High-Performance Open-source, Document-orientated database.
– Built for Speed – the performance of traditional key-value stores while maintaining functionality of traditional RDBMS.
– Rich Document based queries for Easy readability.
– Full Index Support for High Performance.
– Replication and Failover for High Availability.
– Auto Shading for Easy Scalability.
– Map / Reduce for Aggregation.
I will explain above features in this blog on introduction level, after reading this blog you will get familiar to MongoDB
Read this if you want to know, Why choose MongoDB over other Databases?
Operational Database Landscape
Before proceeding deep into 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 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.
Why should we use MongoDB?
We all use programming languages and deal with objects in all most of 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 70’s to store data. The problem with SQL is it’s tabular if you want to store anything you need to map your object to a table and need to write logic for mapping, and 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 is 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 object 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
How does MongoDB store data
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 :
– Store and combine data of any structure
– No need of giving sophisticated validation rules
– You can dynamically modify the schema without downtime
– Fast, Iterative Development
– You spend less time prepping your data for the database, and more time putting your data to work.
What are common use cases for MongoDB?
– The most common use cases for MongoDB include
The Largest Ecosystem
So let us talk about some numbers, I have taken stats from MongoDB official site
– 10,000,000+ MongoDB Downloads
– 300,000+ Online Education Registrants
– 40,000+ MongoDB Cloud Manager Users
– 35,000+ MongoDB User Group Members
– 1,000+ Technology and Services Partners
– 2,000+ Customers Across All Industries
When would RDBMS be a better fit?
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.
SQL Database vs MongoDB
Understanding the Document
Let say you want to store something in MongoDB for an example an object will look like below:
// find posts which have ‘MongoDB’ tag
Some Cool features of MongoDB
Here are couple of cool MongoDB features
– Horizontally Scalable Architectures – no joins + no complex transactions
– Geospatial Indexes for Geospatial queries. $near, $within_distance, Bound queries (circle, box)
– GridFS Stores Large Binary Files.
– Map/Reduce GROUP BY in SQL, map/reduce in MongoDB.
How do we use MongoDB at Habilelabs
We at Habilelabs use with Nodejs, generally as a database of REST API Projects in MEAN stack. We use mongoosejs object modeling which helps us in writing MongoDB validation, casting and business logic boilerplates here is link for mongoosejs : 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.