Turning Learners Into Developers
Codekilla
CODEKILLA
back to course
Lesson 02 / 1571%· free preview
Introduction to MongoDB2/9

What is a Database?

A database is a structured store that persists and organizes data for efficient retrieval and manipulation.

// table = grid of rows × columnsidnameemailcountry1Ashaasha@ex.comIN2Raviravi@ex.comIN3Meimei@ex.comSGClick a step to highlight the table / row / column.
Visual explanation diagram · click steps to walk through it
Problem (from previous lesson)

In the "What is MongoDB?" lesson, we learned MongoDB is a NoSQL database—but we never explored why we need a database at all. Many beginners wonder: "Can't I just store data in text files or spreadsheets?" For small datasets, plain files work. But once you're managing thousands of user records, product inventories, or transaction logs, you face chaos: slow searches, data corruption, no concurrency control, and no structured querying.

Limitation
  • File-based storage becomes unmanageable with large datasets—searching requires reading the entire file, which is O(n) time complexity.
  • No concurrent access—two processes writing to the same file simultaneously can corrupt data.
  • No relationships—linking a customer to their orders in separate text files requires manual parsing and error-prone logic.
  • No security or permissions—anyone with file access can read or modify everything.
Solution (New Concept)

A database solves these issues by providing a structured, persistent store with built-in querying, indexing, concurrency control, and access management. Instead of wrestling with files, you interact with a system designed specifically for data.

Definition

A database is an organized collection of data stored electronically, managed by a Database Management System (DBMS). The DBMS handles data storage, retrieval, updates, and ensures data integrity and security. Databases support structured queries (e.g., "find all users who registered last month") and scale to millions of records efficiently.

Real-Life Example

Think of a database like a library catalog system. Instead of dumping all books in a pile (like files in a folder), the library organizes books by genres, authors, and ISBN numbers. The catalog system (DBMS) lets you search by title, check availability, and even handles multiple people borrowing books simultaneously without conflicts.

Library (File System) Chaotic, slow search Catalog System (Database) Fiction Science History Author A-M Author N-Z ISBN Index Instant search, structured
Key Concepts
  • Persistence — Data survives program restarts and system reboots.
  • Structured storage — Data follows a schema (rigid in RDBMS, flexible in NoSQL).
  • Querying — Use a language (SQL, MQL) to filter, sort, and aggregate data.
  • Concurrency control — Multiple users can read/write simultaneously without conflicts (ACID transactions).
  • Indexing — Speed up searches by creating lookup structures (like a book index).
  • Security — Role-based access control ensures only authorized users can perform certain operations.
ConceptMeaningExample
PersistenceData stored on disk, survives crashesUser profiles remain after server restart
IndexingData structure for fast lookupsB-tree index on email field for quick login
ACID TransactionsAtomicity, Consistency, Isolation, DurabilityBank transfer: debit and credit happen together or not at all
SchemaStructure/rules for dataRDBMS: strict columns; MongoDB: flexible documents
Subtopics
1. Relational Databases (RDBMS)

Relational databases store data in tables with rows and columns. Each row is a record, and columns have fixed data types. Relationships between tables are defined via foreign keys. Examples: PostgreSQL, MySQL, Oracle.

FeatureDescriptionExample
TablesData organized in rows/columnsusers table with id, name, email columns
SQLStructured Query LanguageSELECT * FROM users WHERE age > 25
JoinsCombine data from multiple tablesJOIN orders ON users.id = orders.user_id
Schema-strictMust define columns upfrontAdd a new column requires ALTER TABLE

When to use: Financial systems, ERP, data with complex relationships and strict integrity requirements.

Real-life examples:

  • Banking systems (transactions must be ACID-compliant)
  • E-commerce inventory (products, orders, customers linked via foreign keys)
  • Hospital patient records (strict schema for regulatory compliance)
2. Document Databases (NoSQL)

Document databases store data as JSON-like documents (MongoDB uses BSON). Each document can have a different structure—no fixed schema. Ideal for hierarchical or nested data.

FeatureDescriptionExample
DocumentsSelf-contained JSON objects{ "name": "Alice", "orders": [{...}] }
Flexible schemaFields can vary per documentUser A has address, User B doesn't
Embedded dataNest related data inside one docOrder details embedded in user document
Horizontal scalingSharding across multiple serversDistribute 1 billion docs across 10 shards

When to use: Catalogs, content management, real-time analytics, apps with evolving data models.

Real-life examples:

  • Social media posts (variable fields: images, videos, hashtags)
  • Product catalogs (different attributes per category)
  • IoT sensor data (unpredictable fields)
3. Key-Value Stores

Simplest database type: store data as key-value pairs. Extremely fast for lookups by key but no querying on values. Examples: Redis, DynamoDB.

FeatureDescriptionExample
KeysUnique identifieruser:12345
ValuesArbitrary data (string, binary){ "name": "Bob", "score": 100 }
No queriesCan't search by valueCan't find "all users with score > 50"
In-memoryOften RAM-based for speedRedis stores data in memory, optional persistence

When to use: Caching, session storage, real-time leaderboards, rate limiting.

Real-life examples:

  • Session tokens (key: token, value: user ID)
  • Shopping cart (key: cart ID, value: list of items)
  • Rate limiting (key: IP address, value: request count)
4. Graph Databases

Optimized for relationships. Data is stored as nodes (entities) and edges (relationships). Perfect for social networks, recommendation engines. Examples: Neo4j, Amazon Neptune.

FeatureDescriptionExample
NodesEntitiesUser, Product, Company
EdgesRelationshipsUser FOLLOWS User, User PURCHASED Product
TraversalNavigate relationships efficiently"Friends of friends who like jazz"
Cypher/GremlinQuery languagesMATCH (u:User)-[:FRIEND]->(f) RETURN f

When to use: Social networks, fraud detection, recommendation systems, knowledge graphs.

Real-life examples:

  • LinkedIn connections (degrees of separation)
  • Fraud detection (find suspicious transaction patterns)
  • Movie recommendations ("Users who watched X also watched Y")
5. Comparison: RDBMS vs Document vs Key-Value vs Graph
Database TypeBest ForStrengthsWeaknesses
RDBMSStructured, relational dataACID transactions, SQL joinsRigid schema, harder to scale horizontally
Document (MongoDB)Semi-structured, nested dataFlexible schema, horizontal scalingWeaker joins, eventual consistency
Key-ValueCaching, simple lookupsExtremely fast, simpleNo querying, no relationships
GraphRelationship-heavy dataEfficient traversalComplex queries, harder to scale
Visual Diagram

This diagram shows the four main database types and their core structures:

RDBMS id | name | age 1 | Alice | 30 2 | Bob | 25 Rows & Columns Document DB { "name": "Alice", "age": 30, "orders": [...] } { "name": "Bob", "age": 25 } JSON Documents Key-Value user:123 {data} cart:789 [items] Fast Lookups Graph DB Alice Bob FOLLOWS Carol Nodes & Edges SQL-based MongoDB, Couch Redis, Memcached Neo4j, Neptune Use Case Comparison Financial apps Complex joins ACID critical CMS, Catalogs Flexible schema Horizontal scale Caching Session store Ultra-fast reads Social networks Recommendations Fraud detection
Syntax

Below is basic MongoDB shell syntax for interacting with a document database:

js
// Switch to a database (creates if doesn't exist)
use myDatabase

// Insert a document into a collection
db.users.insertOne({ name: "Alice", age: 30, city: "NYC" })

// Query documents
db.users.find({ age: { $gt: 25 } })

// Update a document
db.users.updateOne({ name: "Alice" }, { $set: { city: "LA" } })

// Delete a document
db.users.deleteOne({ name: "Alice" })
Example

This Node.js script connects to MongoDB, creates a collection, inserts documents, and queries them:

js
const { MongoClient } = require('mongodb');

async function main() {
  const uri = 'mongodb://localhost:27017';
  const client = new MongoClient(uri);

  try {
    await client.connect();
    console.log('Connected to MongoDB');

    const db = client.db('learnDB');
    const users = db.collection('users');

    // Insert documents
    await users.insertMany([
      { name: 'Alice', age: 30, city: 'NYC' },
      { name: 'Bob', age: 25, city: 'SF' },
      { name: 'Carol', age: 35, city: 'LA' }
    ]);
    console.log('Inserted 3 users');

    // Query users older than 26
    const result = await users.find({ age: { $gt: 26 } }).toArray();
    console.log('Users older than 26:', result);

    // Update Alice's city
    await users.updateOne({ name: 'Alice' }, { $set: { city: 'Austin' } });
    console.log('Updated Alice\'s city');

    // Delete Bob
    await users.deleteOne({ name: 'Bob' });
    console.log('Deleted Bob');

    // Final count
    const count = await users.countDocuments();
    console.log('Total users remaining:', count);

  } finally {
    await client.close();
  }
}

main().catch(console.error);
Output
text
Connected to MongoDB
Inserted 3 users
Users older than 26: [
  { _id: ObjectId("..."), name: 'Alice', age: 30, city: 'NYC' },
  { _id: ObjectId("..."), name: 'Carol', age: 35, city: 'LA' }
]
Updated Alice's city
Deleted Bob
Total users remaining: 2
Common Mistakes
MistakeWhy it failsCorrect way
Using text files for large datasetsO(n) search time; no concurrency; data corruption riskUse a database with indexing and transactions
Choosing RDBMS for rapidly changing schemasSchema changes require migrations; downtimeUse document DB (MongoDB) for flexible schemas
Using document DB for complex multi-table joinsMongoDB has limited join support ($lookup is slow)Use RDBMS if your app relies heavily on joins
Storing relationships in key-value storeNo way to query "all friends of user X"Use graph DB for relationship-heavy data
Not indexing frequently queried fieldsFull collection scans on every queryCreate indexes on fields used in queries
Notes & Important Points
  1. Databases are not interchangeable—choosing the wrong type leads to performance bottlenecks and architectural pain. RDBMS excels at structured, relational data; MongoDB at semi-structured, nested data; Redis at caching; Neo4j at relationships.
  2. MongoDB is schema-flexible, not schema-less—you still need to design your document structure. Embedding vs referencing decisions impact performance.
  3. ACID guarantees (Atomicity, Consistency, Isolation, Durability) are critical for financial apps. MongoDB supports multi-document ACID transactions since version 4.0.
  4. Indexing is mandatory for production apps. Without indexes, MongoDB scans every document (O(n) time). Create indexes on fields you filter, sort, or join on.
  5. Horizontal scaling (sharding) is MongoDB's superpower. Unlike RDBMS, MongoDB can distribute data across multiple servers, handling petabytes of data.
  6. Durability settings matter—MongoDB's writeConcern and readConcern control how aggressively data is persisted. Default is safe but adjust for performance-critical apps.
Advantages / Use Cases
  1. Persistence—Data survives crashes, power outages, and application restarts. No risk of losing customer orders or user profiles.
  2. Concurrency control—Multiple users can read/write simultaneously. Databases handle locks, isolation, and prevent race conditions.
  3. Efficient querying—Retrieve exactly the data you need in milliseconds. Compare: file search O(n) vs indexed query O(log n).
  4. Scalability—Modern databases (MongoDB, Cassandra) scale horizontally across thousands of servers, handling billions of records.
  5. Data integrity—Enforce constraints (unique emails, referential integrity). Prevent invalid data from entering the system.
  6. Security—Role-based access control. Only admins can delete; analysts can read; apps can write. File systems lack this granularity.
  7. Backup & recovery—Automated snapshots, point-in-time recovery. If a bug deletes data, restore from 5 minutes ago.
Key Takeaways
  1. A database is a structured, persistent store managed by a DBMS, designed for efficient data retrieval, concurrency, and integrity.
  2. RDBMS (PostgreSQL, MySQL) uses tables and SQL; best for structured data with complex relationships and ACID requirements.
  3. Document databases (MongoDB) store JSON-like documents; best for flexible schemas, nested data, and horizontal scaling.
  4. Key-value stores (Redis) offer ultra-fast lookups by key; best for caching, sessions, and simple data.
  5. Graph databases (Neo4j) optimize for relationships; best for social networks, recommendations, and fraud detection.
Interview Questions

Practice Questions
  1. Design a simple user authentication system. Which database type would you choose (RDBMS, document, key-value, graph) and why? Consider storing usernames, passwords, session tokens, and login history.
  2. Write a MongoDB query to find all users who registered in the last 7 days and live in "NYC". Assume a users collection with fields createdAt (Date) and city (String).
  3. Compare storing blog posts in RDBMS vs MongoDB. If each post has comments, tags, and author info, which approach (embedding vs referencing) would you use in MongoDB?
  4. Explain how you would implement a "trending hashtags" feature on Twitter. Would you use a document DB, key-value store, or graph DB? Justify your choice.
  5. A file-based system stores 10 million user records. Searching for a user by email takes 5 seconds. Estimate the time after adding a B-tree index. Explain the time complexity improvement.
Pro Tips
  1. Start with the data model—before choosing a database, sketch your entities and relationships. If you see many-to-many relationships, consider RDBMS or graph DB. If you see nested documents, MongoDB shines.
  2. Normalize in RDBMS, denormalize in MongoDBRDBMS avoids duplication via foreign keys. MongoDB embraces duplication (embedding) for read performance. Don't fight the paradigm.
  3. Use the right tool for the job—many apps use multiple databases. Store user profiles in MongoDB, cache sessions in Redis, and analyze relationships in Neo4j.
  4. Index early, index often—don't wait until production slows down. Create indexes during development. MongoDB's explain() command shows if a query uses an index.
  5. Monitor query performance—use MongoDB Atlas's performance advisor or mongotop to identify slow queries. A single missing index can cripple your app.
  6. Test with production-scale data—insert 1 million fake documents and benchmark queries. Performance characteristics change dramatically at scale.
  7. Understand CAP theorem—in distributed databases (MongoDB sharding, Cassandra), you can't have Consistency, Availability, and Partition tolerance simultaneously. MongoDB prioritizes CP (consistency + partition tolerance) by default, but you can tune it toward AP (availability).
AI-powered recap

Quick recap quiz?

We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.

# program

Program

MongoDB
const { MongoClient } = require('mongodb');

async function main() {
  const uri = 'mongodb://localhost:27017';
  const client = new MongoClient(uri);

  try {
    await client.connect();
    console.log('Connected to MongoDB');

    const db = client.db('learnDB');
    const users = db.collection('users');

    // Insert documents
    await users.insertMany([
      { name: 'Alice', age: 30, city: 'NYC' },
      { name: 'Bob', age: 25, city: 'SF' },
      { name: 'Carol', age: 35, city: 'LA' }
    ]);
    console.log('Inserted 3 users');

    // Query users older than 26
    const result = await users.find({ age: { $gt: 26 } }).toArray();
    console.log('Users older than 26:', result);

    // Update Alice's city
    await users.updateOne({ name: 'Alice' }, { $set: { city: 'Austin' } });
    console.log('Updated Alice\'s city');

    // Delete Bob
    await users.deleteOne({ name: 'Bob' });
    console.log('Deleted Bob');

    // Final count
    const count = await users.countDocuments();
    console.log('Total users remaining:', count);

  } finally {
    await client.close();
  }
}

main().catch(console.error);
Ready to move on?
// feedback.matters()
Did this lesson help you?