Urgent.News

What's breaking now, across thousands of outlets.

Tech

[quick-revisit]: MongoDB Indexes & B-Trees

The Mental Model A MongoDB collection without an index is like a cookbook without an index section—finding a single recipe requires flipping through all 500 pages. An index extracts a small subset of fields, sorts them, and maps them to exact disk locations so MongoDB can turn directly to the target page and jump straight to the document. The Basics An index is a specialized, memory-friendly data…

MongoDB indexes act like an index in a cookbook, allowing you to quickly locate a single document without scanning through the entire collection. An index is a data structure that stores a subset of fields, sorted, and maps them to disk locations for efficient retrieval. MongoDB uses B-Trees to organize index keys, enabling logarithmic lookup times.

Every collection automatically has a unique index on the _id field, which cannot be dropped and enforces primary key uniqueness. To prevent silent database bugs, follow four key rules:

1. Understand execution stages and look for COLLSCAN (collection scan) or IXSCAN (index scan) stages. TotalDocsExamined should be significantly smaller than nReturned to avoid unnecessary data reads.

2. Follow the ESR Rule for compound indexes: place exact match fields first, sorting fields second, and range filters last. For example, use { status: 1, age: 1 } instead of { age: 1, status: 1 }.

3. Be cautious with embedded documents. Single-field indexes work when querying the entire object, but querying specific sub-fields can skip the index.

4. Consider the write penalty. Indexing takes memory and requires maintenance for every insert, update, or delete operation. High read/low write workloads can benefit from aggressive indexing, while write-heavy collections should minimize indexes to avoid IOPS and storage bloat.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

How to Truly Full-Screen Window in macOS Stage Manager (Even with an External Keyboard!)

Have you ever settled in to work on your Mac, turned on Apple’s Stage Manager to keep your desktop organized, and tried to maximize a window—only to watch macOS stubbornly leave a giant empty strip on…

  • Fn + Control + F shortcut doesn't work with external keyboards in Stage Manager mode
  • Fill mode stretches window edge-to-edge while keeping Stage Manager active
  • Create custom shortcut Control + Option + F in macOS Keyboard settings

Leave a Replay Script, Not a Transcript

A backend engineer opened a pull request at sixteen forty after an afternoon spent steering a coding agent. The ticket claimed the pagination bug was gone, the tests were green, and the cache key no…

  • Replay script captures agent's actions, not understanding
  • Replay script must be reproducible on any machine
  • Spike passes if script runs successfully without original chat directory

More from Saturday 19 September →