MongoDB Tutorial menu

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

javascript
{
  _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 an ObjectId for 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 databaseMongoDB
DatabaseDatabase
TableCollection
RowDocument
ColumnField
Primary key_id field
JOINEmbedded 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

javascript
{ 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:

namecityagemarkscourseskills
Ali KhanKarachi1978Web DevelopmentHTML, CSS
Ayesha SiddiquiLahore2185Python ProgrammingPython
Bilal AhmedKarachi2264Web DevelopmentHTML, JavaScript
Fatima NoorIslamabad2092Graphic DesigningPhotoshop, Illustrator
Hamza RazaKarachi1870(no field)(no field)
Sana IqbalLahore2388Python ProgrammingPython, 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 fitA SQL database may be better
Data whose shape changes oftenData with strict, fixed structure
Data that is read together (a post with its comments)Many relations between many tables
JavaScript and Node.js appsComplex reports with many joins
Fast prototypes and startupsBanking-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?

Learn MongoDB with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp