A dashboard shows a project's latest activity and daily totals. The original implementation embeds every event in the project document. That makes the first read simple, but the history has no natural upper bound. Start the interview with access patterns: recent events for one project, older pages on demand, and a daily breakdown over a bounded interval.

Turn the access patterns into a model

1. Should the project embed its entire history?

Embed data that is bounded and normally read or updated with its parent. An unbounded event list keeps growing, risks the BSON document size limit, and concentrates writes on the same project document. Put activity events in a separate collection keyed by project. A small preview can still be embedded if it is useful and explicitly bounded, but decide whether that preview is authoritative or can lag behind the feed.

2. Which index fits newest-first pages for a project?

Put the equality field first, then the sort fields. The ID provides a tie breaker when timestamps are equal; create the index and match the query's sort direction:

db.events.createIndex({ projectId: 1, occurredAt: -1, _id: -1 });
db.events.find({ projectId: "p1" })
  .sort({ occurredAt: -1, _id: -1 })
  .limit(25);

For a next-page cursor, match timestamps earlier than the last event or the same timestamp with a smaller ID. Retain the BSON date and ID types when decoding the cursor. The order is deterministic for stable records; a cursor alone does not freeze the dataset between requests. An endpoint that sorts across all projects has a different access pattern and should not assume this index can satisfy the sort.

3. Does adding an index mean the query got cheaper?

Inspect explain('executionStats'). Compare documents and keys examined with rows returned, and look for a blocking sort. Measure with project sizes and filters similar to production. An index that makes one read cheap still consumes storage and adds work to writes, so do not index every field just because it can appear on the response.

Keep analytics separate from page retrieval

4. What makes the daily aggregation predictable?

Match the project and time range early, then group timestamps into days and count the input documents. Use $dateTrunc with an explicit business time zone if “day” is not UTC. An initial match can use a suitable index, but grouping still has to process the qualifying events. For a repeatedly requested large range, maintain a summary only after accounting for late-arriving or corrected events and deciding how fresh the summary must be.

5. Is a separate event insert atomic with a project update?

A single document write is atomic. An insert into the events collection does not become atomic with a separate project update merely because both write operations use the same client. If a business invariant requires both, use an appropriate multi-document transaction and account for its overhead. If the second value is only a display hint, it may be better to rebuild it or tolerate lag rather than force every write through that boundary.

Practice on skewed data

  • Draw a project with ten events and another with a million. Explain which writes and reads become expensive.
  • Give two events the same timestamp, then trace page boundaries using both sort fields.
  • Shift the reporting time zone and explain what changes in the date range and grouping.

Compare the approach to checking row grain in SQL, then answer one of the modeling questions aloud before using Mentaro's free practice demo.

Sources