mongodb-expert

Diagnose and optimize MongoDB schema design, aggregation pipelines, indexing, sharding, and connection pools.

3|Updated Jan 15, 2026
One-click install
npx skills add https://github.com/trudyan141/my-antigravity-agents-kit --skill mongodb-expert-trudyan141
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: mongodb-expert
Source: https://github.com/trudyan141/my-antigravity-agents-kit/tree/main/templates/.agent/skills/mongodb-expert
Command: npx skills add https://github.com/trudyan141/my-antigravity-agents-kit --skill mongodb-expert-trudyan141

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? MongoDB applications often suffer from slow queries, unbounded document growth, exhausted connection pools, and poorly chosen shard keys. This Skill provides a structured diagnostic workflow that detects your MongoDB environment, categorizes the issue into one of eight problem areas, and applies progressive fixes with concrete code examples. ## Core Features & Use Cases - Document Modeling & Schema Design: Apply embed-vs-reference decisions, attribute, bucket, computed, and subset patterns while staying under the 16MB document limit. - Performance Optimization: Optimize aggregation pipelines with early $match stages, design compound indexes following the ESR rule, and build covered queries. - Cluster Operations: Configure connection pools, select high-cardinality shard keys, tune replica set read preferences, and handle transactions with retry logic. - Use Case: Your aggregation pipeline times out on a sharded cluster. The Skill profiles the pipeline with explain("executionStats"), reorders stages for index usage, and rewrites the $group to leverage shard key pushdown. ## Quick Start Ask the assistant to analyze why your MongoDB query is slow and to recommend indexes and pipeline optimizations for your collection.

Frequently Asked Questions about mongodb-expert

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

FAQPage Schema
How do I optimize a slow MongoDB aggregation pipeline?

Place $match stages early to filter documents before expensive operations, use $project to reduce document size, and enable allowDiskUse for large datasets. Run explain("executionStats") to verify index usage and design $group stages around shard key fields for pushdown optimization.

How do I choose a MongoDB shard key?

Choose a shard key with high cardinality and even distribution, such as a compound key like { region: 1, customerId: 1, date: 1 }. Avoid low-cardinality fields like status, and include fields from your most common queries to prevent scatter-gather operations.

What is the ESR rule for MongoDB compound indexes?

ESR stands for Equality, Sort, Range—the optimal field order for compound indexes. Place equality-matched fields first, then sort fields, then range-filtered fields, so queries like { status: "active" } sorted by priority with a createdAt range use a single efficient index.

Should I embed or reference related data in MongoDB?

Embed when data is queried together and arrays stay small and bounded; reference when documents grow large, data updates frequently, or relationships are many-to-many. Avoid unbounded arrays on the 'one' side—put references on the 'many' side instead.

Why am I getting MongoDB connection pool exhausted errors?

Pool exhaustion occurs when concurrent operations exceed maxPoolSize or connections are not released. Set maxPoolSize based on peak concurrent operations times 1.2 plus a buffer, configure maxIdleTimeMS to recycle idle connections, and monitor pool events like connectionCheckedOut.

How do I handle TransientTransactionError in MongoDB transactions?

Wrap session.withTransaction in a retry loop that checks error.hasErrorLabel('TransientTransactionError') and retries the transaction. Keep transaction scope minimal, use majority read and write concerns, and always end the session in a finally block.