MongoDB Terminology
Every MongoDB tutorial throws the same five words at you in the first minute. Learn them once, map them to what you already know, and the rest of this course reads easily.
The rule
Databases hold collections, collections hold documents, and every document has an
_id.
| MongoDB | Relational cousin | Bookshop example |
|---|---|---|
| database | database | bookshop |
| collection | table | books |
| document | row | one book |
| field | column | title |
_id |
primary key | 1 |
A few rules make the words precise:
_idis required and unique in its collection. If you insert a document without one, MongoDB adds an ObjectId, a 12-byte value that is unique in practice. You may choose your own_id(this course uses small numbers so examples stay readable). It cannot be changed after insert.- Collections and databases appear on first write.
use bookshopswitches to the name; the first insert creates both. - A collection has no fixed columns. Documents in it usually look alike, but they are allowed to differ.
Here is the running example for the whole course, the books collection:
| _id | title | author | year | price | stock | genres |
|---|---|---|---|---|---|---|
| 1 | Dune | Frank Herbert | 1965 | 9.99 | 12 | sci-fi, classic |
| 2 | Neuromancer | William Gibson | 1984 | 8.5 | 0 | sci-fi, cyberpunk |
| 3 | Emma | Jane Austen | 1815 | 4.25 | 7 | classic, romance |
| 4 | The Hobbit | J.R.R. Tolkien | 1937 | 7.99 | 3 | fantasy, classic, adventure |
| 5 | Circe | Madeline Miller | 2018 | 12 | 5 | fantasy, myth |
Each book also has a publisher object and most have a ratings array; later lessons show those in full.
The shortcut
Read any mongosh command right to left: method, collection, database.
The method
findOne(...)is the action: return one matching document.
use bookshop
db.wishlist.insertOne({ title: "Beloved", author: "Toni Morrison" }) // creates wishlist
show collectionsThe trap: expecting an error when you mistype a collection name. db.bokos.insertOne(...) quietly creates a new collection called bokos.
Drills
Answer fast — trust the rule.
In relational terms, a MongoDB collection is closest to a…
You insert a document with no _id. What happens?
In db.books.countDocuments(), what is books?
You run db.bokos.insertOne({ title: "Emma" }) by mistake. What happens?
Remember
- Databases hold collections, collections hold documents, and every document has a unique
_id. - Shortcut: read mongosh commands right to left, method then collection then database.
- Trap: a typo in a collection name creates a new collection instead of failing.
