What problem does it solve?
Prevents wasteful MongoDB access patterns in Mongoose code by ensuring each document is fetched once, passed through service layers, and queried efficiently (avoiding N+1 queries, looped queries, and overly broad selects).
Core Features & Use Cases
- Hoist and reuse fetched documents: Pass prefetched documents through controller/service chains instead of re-querying by IDs.
- Batch reads and writes: Replace per-item queries in loops with
$in batching, Map-based lookups, and bulkWrite for updates.
- Tighten query performance: Use
.select() for only needed fields, .lean() for read-only paths, and ensure index coverage for filter fields.
- Reduce populate-driven N+1: Avoid
.populate() across lists when it triggers hidden per-document queries; batch manually instead.
Use case: You’re reviewing a checkout/order workflow where controllers call multiple services and each service re-fetches the same Order and related documents—refactor the data flow to fetch once, narrow fields, batch list reads, and write updates efficiently.
Quick Start
Apply the mongodb-query-patterns skill while refactoring a Mongoose endpoint to review data flow, remove duplicate queries, batch list operations, and ensure queries use .select(), .lean(), indexing, and bulkWrite where appropriate.