MongoDB Tutorial for Beginners
Written By: Avinash Malhotra
Updated on
What is MongoDB?
MongoDB is an open-source, document-oriented database designed for modern applications. It stores related data in flexible documents, making it useful when an application needs to evolve quickly, handle high traffic, or scale across multiple servers.
MongoDB stores documents in JSON-like BSON format. A database contains collections (similar to tables in a relational database), and collections contain documents (similar to rows). Documents in the same collection do not have to contain exactly the same fields, although applications should still validate important data and use indexes for common queries.
How MongoDB Stores Data in a Document
{
"_id":"5eab0997f687e3ea1b1ea997",
"name":"user",
"id":"212",
"age":"22",
"address":{
"city":"new delhi",
"pin":"110001"
}
}
This document embeds the address inside the user record. In a relational design, the same data might be split between user and address tables and connected with a key. Embedding is convenient for data that is usually read together; related data can also be referenced when it changes independently or grows without bounds.
NoSQL
NoSQL is a broad category of database systems that do not depend on the traditional relational table model. The name is commonly expanded as "not only SQL": many NoSQL databases support their own query languages, and some also support SQL-like queries.
NoSQL databases can store structured, semi-structured, and relationship data. MongoDB represents relationships with embedded documents or references, depending on how the application reads and updates the data.
NoSQL systems became popular for workloads that benefit from flexible data models, low-latency access, and horizontal scaling. They are not automatically faster or better than relational databases, so the workload and consistency requirements should guide the choice.
NoSQL vs. SQL
| SQL | NoSQL | |
|---|---|---|
| Type | Relational | Non Relational |
| Schemas | Pre Defined | Dynamic |
| Base | Table | key-value, document, graph |
| Scalability | Vertical | Horizontal |
| ACID support | Supported | Supported by MongoDB transactions |
| Joins | Commonly used | Optional; MongoDB uses embedded documents, references, and $lookup |
| Data to Object Mapping | Requires ORM i.e. (object-relational mapping) | Not Required in MongoDB |
SQL to NoSQL
Developers familiar with JavaScript often find MongoDB's JSON-like documents approachable. Developers coming from a relational database will need to learn how collections, documents, indexes, aggregation pipelines, and embedding differ from tables, rows, joins, and SQL queries.
Use this SQL to MongoDB cheatsheet as a starting point, then consult the linked lessons for complete examples.
| 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 (name) VALUES ('avinash') | db.employee.insertOne({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 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.
- Both Cloud and on-premises support.
MongoDB Commands in Mongo shell
| Command | Use |
|---|---|
| mongod | command to start mongodb server |
| mongo | open db in terminal |
| mongosh | current MongoDB shell for connecting to a server |
| 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
MongoDB is a good fit when the application works with nested records, needs a flexible document model, or must distribute read and write traffic across servers. Common examples include:
- Big Data
- Content Delivery Management
- Unstructured Data
- Real Time Analytics
- IoT
- Mobile Apps
- Web Applications
When to choose MongoDB
Choose MongoDB when data is naturally represented as a document, access patterns are known, and the application benefits from embedding related values. Plan indexes around real queries, validate required fields, and use transactions when an operation must update multiple documents atomically.
A relational database may be a better fit for heavily normalized data, complex joins, strict schemas, or reporting workloads that depend on mature SQL tooling. Choose the database based on the data model, consistency needs, and query patterns rather than popularity alone.
Here is a list of popular companies using MongoDB Databases in their applications.
- Adobe
- Forbes
- Toyota
- Ebay
- SAP
- Electronics Arts
- GOV.UK
- GE HealthCare