MongoDB Introduction
MongoDB is a document database: it stores each record as a flexible, JSON-like document.
What is MongoDB?
- MongoDB is a NoSQL database, which means it does not use tables and SQL
- Data is stored as documents, which look like JSON objects
- Documents are grouped into collections, and collections into databases
- It is free to use (Community Edition), and MongoDB Atlas is its cloud service with a free tier
- Its name comes from "humongous", because it was built to handle very large amounts of data
What is a Document?
A document is a set of field: value pairs, like a JavaScript object:
Example
{
_id: ObjectId('6710a1f2c3b4d5e6f7a8b901'),
name: "Ali Khan",
city: "Karachi",
age: 19,
marks: 78,
course: "Web Development",
skills: ["HTML", "CSS"],
contact: { phone: "0300-1112233", email: "ali@example.com" }
}- Values can be text, numbers, true/false, dates, arrays (lists) and even other documents (embedded documents), like
contact - Every document has a unique
_id. If you do not give one, MongoDB creates anObjectIdfor you
MongoDB stores documents in a binary form of JSON called BSON, which adds extra types such as dates and exact decimals.
SQL Words and MongoDB Words
| SQL database | MongoDB |
|---|---|
| Database | Database |
| Table | Collection |
| Row | Document |
| Column | Field |
| Primary key | _id field |
| JOIN | Embedded documents, or $lookup |
Flexible Schema
In a SQL table, every row has the same columns. In MongoDB, documents in one collection can have different fields:
Example
{ name: "Hamza Raza", city: "Karachi", age: 18, marks: 70 }
{ name: "Fatima Noor", city: "Islamabad", age: 20, marks: 92,
course: "Graphic Designing", skills: ["Photoshop", "Illustrator"] }Hamza has not chosen a course yet, so his document simply has no course field.
Flexible does not mean messy. In real apps, keep documents in a collection similar, and use tools such as Mongoose (for Node.js) or MongoDB's schema validation to check the data.
The Sample Collection
This tutorial uses an institute database with a students collection of six documents:
| name | city | age | marks | course | skills |
|---|---|---|---|---|---|
| Ali Khan | Karachi | 19 | 78 | Web Development | HTML, CSS |
| Ayesha Siddiqui | Lahore | 21 | 85 | Python Programming | Python |
| Bilal Ahmed | Karachi | 22 | 64 | Web Development | HTML, JavaScript |
| Fatima Noor | Islamabad | 20 | 92 | Graphic Designing | Photoshop, Illustrator |
| Hamza Raza | Karachi | 18 | 70 | (no field) | (no field) |
| Sana Iqbal | Lahore | 23 | 88 | Python Programming | Python, SQL |
The table is just for reading; in MongoDB each row is a document like the ones above. You will insert them in the insert lesson.
When to Use MongoDB
| MongoDB is a good fit | A SQL database may be better |
|---|---|
| Data whose shape changes often | Data with strict, fixed structure |
| Data that is read together (a post with its comments) | Many relations between many tables |
| JavaScript and Node.js apps | Complex reports with many joins |
| Fast prototypes and startups | Banking-style transactions everywhere |
Both kinds are used widely, and many companies use both.
Exercise
Exercise
In MongoDB, what is the equivalent of a row in a SQL table?
