mongodb-mongoose-patterns

Implements MongoDB schema design, indexing, and pagination patterns for TypeScript applications.

Updated Aug 11, 2026
One-click install
npx skills add https://github.com/DucCuong159/Realtime-chatapp --skill mongodb-mongoose-patterns-duccuong159
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: mongodb-mongoose-patterns
Source: https://github.com/DucCuong159/Realtime-chatapp/tree/main/.agent/skills/mongodb-mongoose-patterns
Command: npx skills add https://github.com/DucCuong159/Realtime-chatapp --skill mongodb-mongoose-patterns-duccuong159

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires mongoose.

What problem does it solve? Building performant MongoDB backends requires avoiding common pitfalls like unindexed queries, N+1 problems, slow offset pagination, and bloated Mongoose documents, which this Skill addresses with proven design patterns. ## Core Features & Use Cases - Schema Design Guidance: Rules for embedding vs referencing data, with a complete TypeScript Message schema example including compound indexes. - Query Optimization: Enforces .lean() for read queries, compound indexing strategies, TTL indexes, and connection pool configuration. - Pagination & Aggregations: Provides cursor-based (keyset) pagination for infinite scroll and aggregation pipelines for chat lists with last-message lookups. - Use Case: When building a chat application's message history endpoint, use this Skill to implement cursor-based pagination backed by a { conversationId: 1, createdAt: -1 } compound index instead of slow skip() offsets. ## Quick Start Use the mongodb-mongoose-patterns skill to design an optimized Mongoose schema and cursor-paginated query for my chat messages collection.

Frequently Asked Questions about mongodb-mongoose-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement cursor-based pagination in MongoDB with Mongoose?

Cursor-based pagination filters records with `createdAt: { $lt: cursor }` and sorts descending, fetching limit+1 items to detect hasMore. This avoids the O(N) scan cost of `skip()` and works with a compound index on `{ conversationId: 1, createdAt: -1 }`.

When should I embed vs reference documents in Mongoose schemas?

Embed data that is 1-to-few, queried together most of the time, and bounded under the 16MB document limit, such as message attachments. Reference data that is 1-to-many or unbounded, like messages in a conversation, and index the reference fields.

Why should I use .lean() in Mongoose queries?

The `.lean()` method returns plain JavaScript objects instead of hydrated Mongoose documents, skipping change tracking and getters. For read-only GET endpoints this is roughly 5x to 10x faster and uses significantly less memory.

How do I create a TTL index in MongoDB for expiring documents?

Create a TTL index with `schema.index({ createdAt: 1 }, { expireAfterSeconds: 3600 })` so MongoDB automatically deletes documents after the specified time. This suits OTPs, password reset tokens, and cache entries.

Why is skip() slow for pagination in large MongoDB collections?

The `skip()` operation forces MongoDB to scan and discard every preceding document, giving O(N) cost that grows with page depth. Keyset pagination using an indexed cursor field keeps lookups at O(log N) regardless of position.