Learn MongoDB
MongoDB is a document-oriented NoSQL database that stores data as flexible, JSON-like BSON documents instead of rows in rigid tables.
What Is MongoDB?
MongoDB is a general-purpose, document-based database designed for modern application development. Rather than forcing your data into a predefined grid of rows and columns, MongoDB stores information as documents — structures that closely resemble the objects you already work with in application code. Each document is written in BSON (Binary JSON), a binary-encoded superset of JSON that adds extra types such as dates, 32/64-bit integers, and binary data while keeping the familiar key-value shape.
Documents and Collections vs Tables and Rows
If you come from a relational background, the fastest way to get oriented is by mapping familiar SQL concepts onto their MongoDB equivalents. A relational table becomes a collection, a single row becomes a document, and a column becomes a field inside that document. The critical difference is that documents in the same collection are not required to share an identical set of fields — one product document might have a discount field while another does not, and no schema migration is needed to allow it.
Core Building Blocks
- Database — a physical container that groups related collections
- Collection — a group of related documents, similar to a table
- Document — a single BSON record, similar to a row
- Field — a key/value pair inside a document, similar to a column
- _id — a unique identifier that every document must have
- BSON types — string, int32/int64, double, boolean, date, array, embedded document, ObjectId, null
A user document
{
"_id": ObjectId("64f1a2b3c4d5e6f7a8b9c0d1"),
"name": "Ana Torres",
"email": "ana@example.com",
"age": 29,
"skills": ["mongodb", "node.js", "react"],
"address": {
"city": "Madrid",
"country": "Spain"
}
}Connect with mongosh
mongosh "mongodb://localhost:27017"
use shop
show collections
db.users.find()Insert and read a document
db.users.insertOne({ name: "Ana Torres", age: 29 })
db.users.find({ name: "Ana Torres" })Exercise: MongoDB Get Started
What type of database is MongoDB?
Frequently Asked Questions
- When should I use MongoDB instead of SQL?
- When documents vary in shape, nest naturally, and are usually read whole, such as product catalogues or event logs. Relational databases remain the better default for data with many relationships and strict integrity requirements, like accounting or bookings.
- Does MongoDB support joins?
- Yes, through the aggregation pipeline's lookup stage, but joins are not what it is designed around. The usual approach is to embed related data in the document so a single read returns everything, which is why the data model matters more here than in SQL.
- Do I need to define a schema in MongoDB?
- Not up front, though you almost always should in practice. Schema validation rules stop malformed documents entering a collection, and application code becomes much simpler when every document has a predictable shape.