Mongodb Tutorial
Written By: Avinash Malhotra
Updated on
What is MongoDB?
Mongo DB is an open source, document-based database based on No SQL for modern applications and cloud based servers. MongoDB is used to build high volume, scalable and performance databases
A document-based or document-oriented database like Mongo DB use JSON like key-value pairs to store data. Mongo DB use collections instead of tables. Each database contains collections ( table in RDBMS ) which contains documents ( columns in RDBMS ). The number of documents in each documents can be different from others.
How Mongodb Stores data in JSON
{
"_id":"5eab0997f687e3ea1b1ea997",
"name":"user",
"id":"212",
"age":"22",
"address":{
"city":"new delhi",
"pin":"110001"
}
}
In above example, MongoDB is using one document, i.e.{} to store all data, but for the same query, RDBMS will create two tables, one for user and another for address.
NoSQL
NoSQL ( Not only SQL ) is used to create Non-relational databases , like MongoDB, CouchDB, Redis, Oracle NoSQL. Some blogs describe NoSQL as Non SQL
, but actually NoSQL is Not Only SQL
.
Some people also says that NoSQL cannot store relationship data. But this term is wrong. Actually NoSQL stores relationship data but can also store non-relational data. This means, NoSQL supports both structures and unstructured data.
NoSQL databases were developed in 2000. NoSQL focus on scalability, performance and simplicity.
NoSQl Vs SQL
SQL | NoSQL | |
---|---|---|
Type | Relational | Non Relational |
Schemas | Pre Defined | Dynamic |
Base | Table | key-value, document, graph |
Scability | Vertical | Horizontal |
ACID Support | Yes | No ( except Mongo DB) |
Joins | Required | Not Compulsory |
Data to Object Mapping | Requires ORM i.e. (object-relational mapping) | Not Required in MongoDB |
SQL to NoSQL
MongoDB was developed in 2000. JavaScript Developers will learn MongoDB very easily as MongoDB is JSON based. But RDBMS developer might find MongoDB little difficult to understand.
Here is a cheatsheet for SQL to NoSQL.
SQL | MongoDB |
---|---|
Database | Database |
Table | Collection |
Row | Document |
Column | Field |
Index | Index |
Joins | $lookup |
SELECT * from tablename | db.collections.find() |
SELECT * from tablename LIMIT 1 | db.collections.findOne() |
CREATE TABLE employee ( name VARCHAR(20)) | db.createCollection('employee') |
INSERT INTO employee VALUES ( avinash) | db.employees.insert({name:'avinash'}) |
MongoDB advantages
The Advantage of MongoDB over RDBMS is that Mongodb is document based, schema less, fast and scalable.
- MongoDB is document database. Each collection can have as many documents to store data.
- Schema less database. No need to define length of data and datatype.
- Scalable very easy to scale.
- Document Oriented database that stores data in JSON or BSON
- Joins support, but not compulsory.
- Powerful Query language.
- Full ACID transactions.
MongoDB Commands in Mongo shell
Command | Use |
---|---|
mongod | command to start mongodb server |
mongo | open test database in command prompt |
show dbs | show all available Databases |
use mydb | select mydb Database. If mydb does not exists, it will create mydb |
show collections | show all collections in database selected |
db | check current database |
db.mydb.help() | show all methods used with database |
db.dropDatabase() | to drop or delete existing database |
Where to use MongoDB
Here are the primary areas where MongoDB is preferred.
- Big Data
- Content Delivery Management
- Unstructured Data
- Real Time Analytics
- IoT
- Mobile Apps
- Web Applications
Companies Using MongoDB
List of companies using MongoDB
- Adobe
- Ebay
- SAP
- Electronics Arts
- GOV.UK