Databases

MongoDB

The most popular document NoSQL database, storing data as JSON-like documents (BSON).

What it is

MongoDB stores data as documents in collections, not rows in tables. A document has a flexible structure, can hold nested objects and arrays, and the schema can change without migrations.

It has a rich query language and aggregation pipeline, indexes, multi-document transactions, replication (replica sets) and sharding for horizontal scaling. Atlas is the managed cloud version.

The SSPL license is not open source by the OSI definition: cloud providers cannot offer it as a service without conditions. Using it inside your own application has no restrictions.

Use it when

  • Data with a changing structure: catalogs, content, profiles.
  • A fast prototype where the schema still evolves.
  • You need horizontal scaling with sharding.

Avoid it when

  • Many relations between entities and complex joins: a relational database.
  • Strict financial invariants across many tables.

Key concepts

Document / Collection
A BSON document and a group of documents.
Aggregation pipeline
A sequence of stages like $match, $group, $lookup.
Replica set
A group of nodes with one primary and replicas.
Sharding
Spreading a collection across nodes by a shard key.

Example

Top 5 users by paid order total.

Cheat sheet

MongoDB cheat sheet
Command or codeWhat it does
mongosh "mongodb://localhost:27017"Connect to the server
use shopSelect a database (created on first write)
db.users.insertOne({ name: "Ann", age: 30 })Insert a document
db.users.find({ age: { $gt: 25 } })Find documents by a condition
db.users.updateOne({ name: "Ann" }, { $set: { age: 31 } })Update a field in a document
db.users.deleteOne({ name: "Ann" })Delete one document
db.users.createIndex({ email: 1 }, { unique: true })Unique index on a field
db.users.find({ age: 30 }).explain("executionStats")Query plan and statistics
db.orders.aggregate([{ $match: { paid: true } }, { $group: { _id: "$userId", sum: { $sum: "$total" } } }])Aggregation: order total per user
mongodump --db shop --out ./backupBack up a database

How to learn it

  1. Understand the document model: collections, BSON documents, when it beats tables and when it does not.
  2. Run MongoDB in Docker or Atlas and connect with mongosh or Compass.
  3. Learn CRUD: insertOne, find, updateOne, deleteOne and the operators $set, $inc, $push.
  4. Learn filters and projections: $gt, $in, $and, $regex, plus sorting and pagination.
  5. Design a schema: embed or reference, and why that depends on access patterns.
  6. Add indexes (single and compound), read explain() and remove slow collection scans.
  7. Write an aggregation pipeline with $match, $group, $lookup, $sort.
  8. Learn replica sets, transactions and connecting through a driver or Mongoose.

Interview questions

  1. 1. When to embed documents and when to reference?

    Embed data that is read together and has bounded size. Use references for large, shared or frequently changing entities.

  2. 2. Does MongoDB support transactions?

    Yes, multi-document ACID transactions exist since version 4.0. Still, the document model usually lets you rely on atomic operations on a single document.

  3. 3. What is a shard key and why does it matter?

    It is the field data is distributed by across shards. A bad key causes load skew (a hot shard) and slow scatter-gather queries.

Interview questions from the bank

Comparisons with this technology

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).