MongoDB Intro
MongoDB is a document database. Instead of storing data in tables with fixed columns, it stores documents: JSON-like records made of field/value pairs that can contain numbers, text, dates, lists and even other documents. Related documents live together in a collection, and collections live in a database.
Here is what a single product looks like in MongoDB:
{
_id: 10,
name: 'Pour-Over Dripper',
category: 'equipment',
price: 29,
tags: ['brewing'],
specs: { material: 'ceramic', color: 'white', weightGrams: 450 }
}Notice the tags list and the nested specs object. In a relational database you would probably need extra tables for those. In MongoDB they simply belong to the document.
Why developers use MongoDB
| Feature | What it means for you |
|---|---|
| Flexible documents | Two documents in the same collection may have different fields. You can add a field without a migration. |
| Natural mapping to code | A document looks like an object in JavaScript, a dict in Python or a struct in Go. |
| Rich queries | Filter on any field, inside arrays and nested documents, with operators like $gt and $in. |
| Aggregation pipeline | Group, join, reshape and summarise data on the server in a series of stages. |
| Indexes | Single-field, compound, multikey (arrays), text and geospatial indexes keep queries fast. |
| Scaling | Replica sets copy data for high availability; sharding spreads big collections over many machines. |
SQL words vs MongoDB words
If you already know SQL, this table translates the vocabulary:
| SQL | MongoDB |
|---|---|
| database | database |
| table | collection |
| row | document |
| column | field |
| primary key | the _id field |
JOIN |
embedded documents, or $lookup in an aggregation |
GROUP BY |
the $group aggregation stage |
Your first commands
In this course every example is a mongosh script. mongosh is MongoDB's official shell: a JavaScript environment connected to a database through the variable db. The example below adds one document to a new collection called notes and reads it back:
db.notes.insertOne({ _id: 1, text: 'Learn MongoDB', done: false });
print(db.notes.findOne({ _id: 1 }));{ _id: 1, text: 'Learn MongoDB', done: false }
There was no CREATE TABLE step. The collection notes came into existence the moment the first document was inserted into it.
Every example also starts with a small sample store already loaded: coffee, tea, brewing equipment, customers and their orders. You will meet it properly in The Sample Database. For now, here is how many documents each collection holds:
print('products: ', db.products.countDocuments());
print('customers:', db.customers.countDocuments());
print('orders: ', db.orders.countDocuments());products: 20
customers: 12
orders: 20
Documents can differ
Because there is no fixed schema, documents in one collection don't have to look the same. Here two customers are stored, one with a phone number and one without:
db.people.insertMany([
{ _id: 1, name: 'Mia', phone: '555-0101' },
{ _id: 2, name: 'Leo', languages: ['en', 'es'] }
]);
print(db.people.find().toArray());[
{ _id: 1, name: 'Mia', phone: '555-0101' },
{ _id: 2, name: 'Leo', languages: [ 'en', 'es' ] }
]
That flexibility is powerful, but it is also a responsibility: your application has to cope with missing fields. Later you will see how schema validation lets the server enforce rules when you want them.
Note: "Schemaless" does not mean "no design". Good MongoDB schemas are designed around how the application reads data. The data modeling lesson covers this.
How the examples work on this site
MongoDB is a server program, and a browser cannot run it. So the mongosh run playgrounds on this site are simulated: when you press Run you see the output that the script produced when it was executed against a real MongoDB 8.0 server with mongosh 2.13. Every example ran in its own fresh database with the sample data loaded, so you can copy any script into your own mongosh and get the same result.
Test yourself
In MongoDB, what is the equivalent of a row in a SQL table?
- A collection
- A document
- A field
- An index
What happens when you insert a document into a collection that doesn't exist yet?
- An error: create the collection first
- MongoDB creates the collection automatically
- The document goes into a default collection
- Nothing is stored
Which field acts as the primary key of every MongoDB document?
idkey_id$id