/documentdb:documentdb-query-optimizerfrom documentdb
Helps optimize DocumentDB/MongoDB queries and indexing on Azure DocumentDB. Diagnoses slow queries, reviews indexes, and recommends indexing strategies using DocumentDB MCP tools.
Ships in documentdb. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Invoke only when the user wants:
Do not invoke for routine query authoring unless the user has requested help with optimization, slow queries, or indexing.
If the user is asking about a particular query:
list_indexes (MCP) or db.<coll>.getIndexes() (mongosh) to get existing indexes on the collectionexplain_operation (MCP) or .explain("executionStats") (mongosh)
to get explain output with execution statsfind_documents (MCP) or db.<coll>.findOne() (mongosh) to fetch a sample document to understand the
schemaThen make an optimization suggestion based on collected information and best practices from the reference files. Prefer creating an index that fully covers the query if possible.
If the user wants to examine slow queries or is looking for general performance suggestions (not regarding any particular query):
list_databases (MCP) or show dbs (mongosh) to understand the database structureget_statistics with scope "collection" (MCP) or db.collection.stats() (mongosh) to identify large collectionsget_statistics with scope "index" (MCP) or db.collection.aggregate([{$indexStats:{}}]) (mongosh) to check existing index usagecurrent_ops (MCP) or db.currentOp() (mongosh) to see currently running operationsWhen DocumentDB MCP server is connected, these tools are available:
| Tool name (exact) | Description |
|---|---|
list_indexes | List all indexes on a collection — check if the query can use an existing index |
explain_operation | Run explain with executionStats for any operation (find, aggregate, count) |
find_documents | Fetch sample documents to understand schema — use with limit=1 |
get_statistics | Get collection or index statistics (use scope: "collection" or "index") |
current_ops | Get currently running database operations |
create_index | Create a new index (only after user approval) |
drop_index | Drop an existing index (only after user approval) |
sample_documents | Sample random documents from a collection |
All diagnostic operations can be performed directly via mongosh. Use these when MCP is not available or for quick ad-hoc diagnostics:
// Explain a find query
db.collection.find({filter}).explain("executionStats")
// Explain an aggregation pipeline
db.collection.aggregate([{$match:...}, {$group:...}]).explain("executionStats")
// Explain a count
db.runCommand({explain: {count: "collection", query: {filter}}, verbosity: "executionStats"})
// Collection statistics
db.collection.stats()
// List indexes
db.collection.getIndexes()
// Index usage statistics
db.collection.aggregate([{$indexStats: {}}])
// Sample documents
db.collection.aggregate([{$sample: {size: 3}}])
// Currently running operations
db.currentOp()
Before beginning diagnosis and recommendation, load reference files.
Always load:
references/core-indexing-principles.mdFor a specific query, run these tools:
Via MCP (when connected):
list_indexes({ db_name: "<db>", collection_name: "<coll>" })
explain_operation({
db_name: "<db>",
collection_name: "<coll>",
operation: {
find: "<coll>",
filter: <filter>,
sort: <sort>,
projection: <projection>,
limit: <n>
}
})
For aggregation pipelines:
explain_operation({
db_name: "<db>",
collection_name: "<coll>",
operation: {
aggregate: "<coll>",
pipeline: <pipeline_array>,
cursor: {}
}
})
Via mongosh (no MCP):
use <db>
db.<coll>.getIndexes()
db.<coll>.find(<filter>).sort(<sort>).explain("executionStats")
db.<coll>.aggregate(<pipeline>).explain("executionStats")
From the explain("executionStats") response (via MCP explain_operation or
direct mongosh), extract:
totalKeysExamined, totalDocsExamined, nReturned,
executionTimeMillisKey ratios to evaluate:
| Metric | Good | Bad |
|---|---|---|
| keysExamined / nReturned | Close to 1 | >> 1 (poor selectivity) |
| docsExamined / nReturned | Close to 1 | >> 1 (scanning too many docs) |
| Plan stage | IXSCAN | COLLSCAN (no index) |
| Sort stage | In-memory: false | In-memory: true (blocking sort) |
Common issues and their root causes:
Follow the ESR Rule (Equality → Sort → Range) for compound index design:
$eq / exact match)sort specification)$gt, $lt, $gte, $lte, $in)Example:
Query: db.orders.find({status: 'shipped', region: 'US'}).sort({date: -1})
Recommended index: {status: 1, region: 1, date: -1}
(Two equality fields, then sort field)
After creating the recommended index, re-run the explain to confirm improvement:
explain_operation (MCP) or .explain("executionStats") (mongosh) with the same queryUser: "Why is this query slow?
db.orders.find({status: 'shipped', region: 'US'}).sort({date: -1})"
If MCP connection is available, run steps 1–3:
Check existing indexes:
list_indexes with database=store, collection=orders{_id: 1}, {status: 1}, {date: -1}Run explain:
explain_operation with operation={find: "orders", filter: {status: "shipped", region: "US"}, sort: {date: -1}}{status: 1} index, then in-memory SORT,
totalKeysExamined: 50000, nReturned: 100Fetch sample:
find_documents with limit=1 to understand the schemaIf MCP is not available, use mongosh directly:
use store
db.orders.getIndexes()
db.orders.find({status: "shipped", region: "US"}).sort({date: -1}).explain("executionStats")
db.orders.findOne()
Diagnose: This query targets 100 docs but scans 50K index entries (poor
selectivity: 0.002). In-memory sort adds overhead. The {status: 1} index
doesn't support both filter fields or sort.
Recommend: Create compound index {status: 1, region: 1, date: -1}
following ESR (two equality fields, then sort). This eliminates in-memory
sort and improves selectivity.
_id indexWhat SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub azure/documentdb-agent-kit --plugin documentdb17 skills in this plugin
/documentdb-azure-deploymentDeploy an Azure DocumentDB cluster (`Microsoft.DocumentDB/mongoClusters`) end-to-end — Bicep (primary), Azure CLI (`documentdb` extension), Terraform, or the Azure portal. Every step has a scriptable command, so it works the same for a developer at a terminal, a CI pipeline, or a coding agent. Covers resource-group creation, cluster parameters (tier, storage, server version, sharding, HA), firewall rule configuration, retrieving the connection string, and teardown. Use when the user asks to provision, create, deploy, or spin up an Azure DocumentDB cluster, or wants infrastructure-as-code for one./documentdb-connectionOptimize MongoDB client connection configuration (pools, timeouts, patterns) for Azure DocumentDB. Use this skill when working on functions that instantiate or configure a MongoDB client (e.g., calling `connect()`), configuring connection pools, troubleshooting connection errors (ECONNREFUSED, timeouts, pool exhaustion), optimizing connection-related performance issues. Includes scenarios like building serverless functions, creating API endpoints, optimizing high-traffic applications, or debugging connection failures./documentdb-data-modelingData modeling patterns for Azure DocumentDB — embed vs reference, 16 MB document limit, denormalization for read-heavy workloads, schema versioning. Use when designing new schemas, reviewing existing data models, migrating from SQL, deciding between embedding and referencing, modeling one-to-one / one-to-many / many-to-many relationships, or troubleshooting document-size and query-performance problems that stem from the data model.Other plugins' skills for the same job