mongodb-usage
npx machina-cli add skill fcakyon/claude-codex-settings/mongodb-usage --openclawMongoDB Best Practices
MCP Limitation
This MCP operates in READ-ONLY mode. No write, update, or delete operations are possible.
Schema Design Patterns
Embedding vs Referencing
Embed when:
- Data is accessed together frequently
- Child documents are bounded (won't grow unbounded)
- One-to-few relationships
- Data doesn't change frequently
Reference when:
- Data is accessed independently
- Many-to-many relationships
- Documents would exceed 16MB limit
- Frequent updates to referenced data
Common Patterns
Subset pattern: Store frequently accessed subset in parent, full data in separate collection.
Bucket pattern: Group time-series data into buckets (e.g., hourly readings in one document).
Computed pattern: Store pre-computed values for expensive calculations.
Index Strategies
Index Guidelines
- Index fields used in queries, sorts, and aggregation $match stages
- Compound indexes support queries on prefix fields
- Covered queries (all fields in index) are fastest
- Too many indexes slow writes
Index Types
- Single field: Basic index on one field
- Compound: Multiple fields, order matters for queries
- Multikey: Automatically created for array fields
- Text: Full-text search on string content
- TTL: Auto-expire documents after time period
ESR Rule
For compound indexes, order fields by:
- Equality (exact match fields)
- Sort (sort order fields)
- Range (range query fields like $gt, $lt)
Aggregation Pipeline
Performance Tips
- Put
$matchand$projectearly to reduce documents - Use
$limitearly when possible - Avoid
$lookupon large collections without indexes - Use
$facetfor multiple aggregations in one query
Common Stages
// Filter documents
{ $match: { status: "active" } }
// Reshape documents
{ $project: { name: 1, total: { $sum: "$items.price" } } }
// Group and aggregate
{ $group: { _id: "$category", count: { $sum: 1 } } }
// Sort results
{ $sort: { count: -1 } }
// Join collections
{ $lookup: { from: "orders", localField: "_id", foreignField: "userId", as: "orders" } }
Connection Best Practices
Connection String Formats
- Atlas:
mongodb+srv://user:pass@cluster.mongodb.net/database - Local:
mongodb://localhost:27017/database - Replica set:
mongodb://host1,host2,host3/database?replicaSet=rs0
Connection Pooling
- Use connection pooling in applications (default in drivers)
- Set appropriate pool size for your workload
- Don't create new connections per request
Anti-Patterns to Avoid
- Unbounded arrays: Arrays that grow without limit
- Massive documents: Documents approaching 16MB
- Too many collections: Use embedding instead
- Missing indexes: Queries doing collection scans
- $where operator: Use aggregation instead for security
- Storing files in documents: Use GridFS for large files
Source
git clone https://github.com/fcakyon/claude-codex-settings/blob/main/plugins/mongodb-tools/skills/mongodb-usage/SKILL.mdView on GitHub Overview
This skill enables read-only MongoDB interactions such as querying data, listing databases and collections, inspecting collection schemas, and checking indexes. It emphasizes best practices for schema design, indexing, aggregation, and connection management to help you get accurate results without altering data.
How This Skill Works
In read-only MCP mode, the skill guides you through practical patterns for querying and inspecting MongoDB. It covers embedding vs referencing decisions, index strategies (including compound indexes and ESR ordering), and common aggregation tips, plus safe connection practices to fetch data and analyze structures without writes.
When to Use It
- You need to query records in MongoDB to retrieve specific documents or aggregates
- You want to list databases and show their collections to understand your data landscape
- You need to inspect a collection's schema or typical document shape
- You want to check or optimize database indexes for query performance
- You plan to run simple aggregations while validating results and avoiding writes
Quick Start
- Step 1: Identify the target database and collection you want to query or inspect (read-only access only)
- Step 2: Run a simple read query with projection, then review index usage and ensure relevant indexes exist
- Step 3: Review aggregation steps (e.g., $match, $project, $group) and verify results without performing writes
Best Practices
- Index fields used in queries, sorts, and aggregation stages (incl. $match) and prefer covered queries when possible
- Use the ESR rule to order compound indexes: equality first, then sort, then range
- Choose index types appropriate for the workload (single field, compound, multikey, text, TTL)
- Design schemas with embedding for one-to-few, frequently co-accessed data, or references for independent data
- Limit writes and avoid unbounded arrays and 16MB document sizes; rely on aggregation and GridFS for large files
Example Use Cases
- Query active orders and group by status to monitor order health without modifying data
- List all databases and their collections to map the data landscape in a cluster
- Inspect a collection’s common fields to infer a practical schema and decide embedding vs referencing
- Check an index on users by email and status, adjust compound order per ESR, and observe query performance
- Run an aggregation that matches recent sales, projects needed fields, and groups totals without changing documents