MongoDB & Mongoose
Document modeling, aggregation, indexes, and consistency.
How do indexes help, and what do they cost?
An index is an extra data structure that helps the database find matching records without inspecting every document. Reads can become much faster, but inserts, updates and deletes must maintain the relevant indexes. Indexes are most useful when their fields and order match important query patterns.
What index types should I know?
MongoDB offers different index types because equality lookup, array search, text search, uniqueness and expiry have different needs. A compound index orders several fields together, while a partial index includes only documents matching a condition. TTL indexes support eventual cleanup rather than precise scheduling.
What is the ESR rule?
ESR stands for Equality, Sort and Range, a common starting point for ordering fields in a compound index. Equality fields narrow the relevant index region, sort fields can let MongoDB read in the desired order, and range fields constrain intervals. It is a guideline that must be checked against selectivity and the query plan.
What should I check in explain?
explain shows how MongoDB plans a query, and execution-statistics modes also show what work was performed. It helps distinguish an efficient index lookup from broad scanning, sorting or excessive document fetching. The goal is to understand why the query does work, not simply whether an index was selected.
Difficulty is an editorial estimate. Completion means you chose to mark a question as understood.