cchakri.aiLearn All courses
// COURSE · DATABASES

Database with MongoDB

Learn document modeling, CRUD, aggregation, indexing, replication, sharding, and application integration.

01

Documents and data modeling

Syllabus

  1. 01Documents, collections, and BSON
  2. 02Embedding versus referencing
  3. 03Schema validation and evolution

Detailed content

Documents, collections, and BSON

MongoDB stores BSON documents in collections. Documents can contain nested objects and arrays, making related data convenient to retrieve together. BSON supports typed values such as dates and binary data. Choose fields and types deliberately, and keep document size and shape predictable for the application.

Embedding versus referencing

Embed data when it is read and updated with its parent and has bounded growth. Reference data when it is shared, large, or independently updated. This decision shapes query count, document size, and update complexity. Model around real access patterns, while preserving a clear source of truth for shared entities.

Schema validation and evolution

Flexible documents do not mean schema-free applications. Define required fields, types, and allowed values with validation rules. When a model changes, plan how old documents will be read or migrated. Use version fields or compatible defaults when a rolling deployment must support more than one document shape.

02

CRUD and aggregation

Syllabus

  1. 01Filtering and CRUD operations
  2. 02Aggregation pipelines
  3. 03Atomic updates and transactions

Detailed content

Filtering and CRUD operations

Find documents with filters that match indexed fields and use projections to return only needed data. Insert, update, and delete operations should include precise selectors. Operators such as $set and $inc express partial updates; replacement updates can accidentally remove fields. Check matched and modified counts for write outcomes.

Aggregation pipelines

An aggregation pipeline passes documents through ordered stages such as $match, $project, $group, and $sort. Put selective filters early when possible, and understand how grouping changes document shape. Pipeline stages can perform joins or reshape data, but expensive stages need realistic limits and indexes.

Atomic updates and transactions

A single-document write is atomic, which is useful when related state is embedded together. Multi-document transactions are available when an invariant truly spans documents, but they add coordination and performance cost. Prefer a model that keeps common updates within one document and use transactions for necessary multi-record guarantees.

03

Indexes and scaling

Syllabus

  1. 01Indexes for query patterns
  2. 02Replica sets and failover
  3. 03Sharding and partition keys

Detailed content

Indexes for query patterns

Create indexes to support actual filters, sorts, and joins. Compound index order determines which prefixes can be used; multikey indexes support array fields with constraints. Explain plans show whether a query scans documents or uses an index. Every index increases storage and write cost, so remove unused indexes.

Replica sets and failover

A replica set maintains copies of data and elects a primary for writes. Secondaries replicate changes and can serve reads according to read preference, with possible staleness. Write concern defines acknowledgment durability; read concern defines visibility guarantees. Understand elections and retries so applications handle failover without duplicating unsafe actions.

Sharding and partition keys

Sharding distributes documents across multiple machines using a shard key. A poor key can create hot spots or scatter every query across shards. Choose a key with useful cardinality and write distribution, and test common queries for targeted routing. Sharding increases operational complexity and should follow evidence that a replica set is insufficient.

04

Application integration and operations

Syllabus

  1. 01Node driver and connection management
  2. 02Security and access control
  3. 03Backups, monitoring, and recovery

Detailed content

Node driver and connection management

Create a reusable MongoDB client and connection pool rather than opening a new connection per request. Use the driver’s typed operations, parameterize filters through application data, and handle timeouts. Close the client during graceful shutdown. In serverless environments, account for instance concurrency and connection limits.

Security and access control

Require authentication, grant each application identity only the database roles it needs, and use TLS for connections. Keep credentials in a secret manager and rotate them safely. Validate user-controlled filters to prevent unintended query operators. Encrypt backups and avoid logging entire documents that may contain personal data.

Backups, monitoring, and recovery

Monitor query latency, replication lag, storage, connection pools, and cache pressure. Configure backups for the required recovery point and test restores into an isolated environment. Watch slow query logs and index effectiveness. A recovery plan should name who acts, how consistency is checked, and how traffic returns after an incident.