MongoDB Fundamentals
Database, Collection, Document and Field
The four nouns you will use in every query, mapped to how data is actually stored.
BeginnerAbout 4 minutes
Every query you write names these four things, whether you think about them or not.
- 01MongoDB server
- 02Database — app
- 03Collection — users, orders, products
- 04Document — Rahul
- 05Field — city, skills, isActive
use app
db.users.findOne({ name: "Rahul" }){
_id: ObjectId("64a1b2c3d4e5f6a7b8c90001"),
name: "Rahul",
age: 28,
city: "Bangalore",
role: "developer",
skills: ["Node.js", "MongoDB"],
isActive: true
}- Database — the application-level container. One backend usually has one (maybe a second for tests). Named
apphere. - Collection — a group of related documents.
users,orders,products,reviews,employees,departments. Close to a table, without a fixed set of columns. - Document — a single record. Rahul is one document in
users. - Field — a property on that document.
cityis a field;skillsis a field whose value is an array.
In SQL: database → table → row → column. In MongoDB: database → collection → document → field. The next two lessons are about what those field values actually are (_id, dates, decimals).
Interview question
How are database, collection, document and field related?
Think about it first.
A MongoDB server holds databases. A database holds collections. A collection holds documents. A document is a set of fields. It maps to database, table, row and column in SQL, with the difference that a document can nest objects and arrays.