Module Review

You have moved beyond basic SQL comparisons. You now understand that Documents are not just JSON blobs—they are BSON structures optimized for Data Locality and atomic operations.

1. Key Takeaways

  • Documents vs Rows: MongoDB stores data in BSON (Binary JSON). This allows for rich types (Date, Int64) and faster traversal than standard JSON.
  • Locality is King: Embedding related data (Addresses inside Users) allows for O(1) reads. Relational joins require O(N*M) seeks.
  • Atomicity: Writes are atomic at the document level. A single updateMany operation is not atomic across all documents.
  • Write Concern: You can trade latency for durability. w:1 is fast (Primary only). w:majority is safe (Replicated).
  • Arrays are Powerful: Use $push, $pull, and $elemMatch to model 1:N relationships without join tables.
  • Upserts: The efficient way to “Create or Update” in a single round trip.

2. Interactive Flashcards

Test your recall of the core concepts.

What is BSON?

Click to reveal

Binary JSON. It is the binary serialization format used by MongoDB. It extends JSON with types like Date, ObjectId, and Binary data, and includes length prefixes for fast traversal.

What is Write Concern `w: majority`?

Click to reveal

It ensures the write is acknowledged by the Primary and a majority of Secondaries. This guarantees the data will not be rolled back during a failover.

When should you use `$elemMatch`?

Click to reveal

When you need to match multiple conditions on a single element within an array of objects. Simple dot notation checks conditions independently across the entire array.

Is `updateMany` atomic?

Click to reveal

No. It is atomic for each individual document it modifies, but the batch operation as a whole is not atomic. If the server crashes halfway, half the documents remain unchanged.

Why is Data Locality important?

Click to reveal

It minimizes disk seeks. By embedding related data (e.g., user + address), the database can read everything in a single sequential I/O operation instead of performing random seeks to join tables.

What is an Upsert?

Click to reveal

An operation that updates a document if it matches the filter, or creates a new one if it doesn't. It makes operations idempotent and simplifies logic.


3. Cheat Sheet

Create & Read

Method Description
insertOne(doc) Insert a single document.
insertMany([docs]) Insert multiple documents in one batch.
find(filter) Return a cursor to matching documents.
findOne(filter) Return the first matching document.

Update Operators

Operator Description
$set Sets the value of a field.
$inc Increments a numeric value.
$unset Removes a field.
$push Appends to an array.
$pull Removes from an array.
$addToSet Adds to array only if unique.

Query Selectors

Operator Description
$eq, $ne Equal, Not Equal.
$gt, $lt Greater Than, Less Than.
$in, $nin In Array, Not In Array.
$exists Matches documents that have the specified field.
$type Selects documents if a field is of the specified type.
$elemMatch Matches elements in an array that satisfy multiple criteria.

4. Next Steps

Now that you can move data in and out, the next question is: How should I structure my documents?

Should you embed or reference? How do you handle 1:1,000,000 relationships?

Proceed to Module 03: Data Modeling to learn the art of schema design.

[!TIP] Need a refresher on terms? Check the MongoDB Glossary.