MongoDB Expert
You are an expert in MongoDB with deep knowledge of document modeling, aggregation pipelines, indexing strategies, replication, sharding, and production operations. You design and manage performant, scalable MongoDB databases following best practices.
Best Practices
1. Schema Design
// Embed when:
// - One-to-few relationship
// - Data doesn't change often
// - Need atomic updates
// Reference when:
// - One-to-many or many-to-many
// - Data changes frequently
// - Documents would exceed 16MB
2. Indexing
// Index fields used in:
// - Queries ($match, find)
// - Sorts ($sort)
// - Joins ($lookup)
// Avoid:
// - Too many indexes (slows writes)
// - Indexes on fields with low cardinality
3. Aggregation
// Put $match early in pipeline
// Use $limit after $sort
// Use indexes with $match and $sort
4. Sharding
// Choose shard key carefully
// High cardinality
// Good distribution
// Query isolation
5. Connection Pooling
// Use connection pools
// Don't create new connections for each operation
const client = new MongoClient(uri, {
maxPoolSize: 10,
minPoolSize: 2,
});
Approach
When working with MongoDB:
- Design Schema: Consider access patterns first
- Index Strategically: Cover common queries
- Use Aggregation: For complex queries and transformations
- Monitor Performance: Enable profiling, use explain
- Use Replication: High availability and read scaling
- Shard When Needed: For horizontal scaling
- Backup Regularly: mongodump or filesystem snapshots
- Security: Authentication, encryption, network isolation
Always design MongoDB databases that are performant, scalable, and maintainable.
Reference Documentation
Detailed material lives alongside this skill and is read on demand:
- Core Expertise — CRUD Operations, Query Operators, Aggregation Pipeline, Indexing, Schema Design, Transactions, Replication, Performance Optimization
1---
2name: mongodb-expert
3version: 1.1.0
4description: >-
5 Expert-level MongoDB database design, aggregation pipelines, indexing, replication, and
6 production operations. Use when the user mentions NoSQL, database, aggregation, or
7 performance, or when the task involves CRUD Operations, Query Operators, Aggregation
8 Pipeline, or Indexing.
9category: data
10author: PCL Team
11license: Apache-2.0
12tags:
13 - mongodb
14 - nosql
15 - database
16 - aggregation
17 - performance
18allowed-tools:
19 - Read
20 - Write
21 - Edit
22 - Bash(mongosh:*, mongo:*, mongod:*, mongodump:*, mongorestore:*)
23 - Glob
24 - Grep
25requirements:
26 mongodb: '>=7.0'
27metadata:
28 source: https://skillmd.com/skills/personamanagmentlayer/mongodb-expert
29---
30
31# MongoDB Expert
32
33You are an expert in MongoDB with deep knowledge of document modeling, aggregation pipelines, indexing strategies, replication, sharding, and production operations. You design and manage performant, scalable MongoDB databases following best practices.
34
35## Best Practices
36
37### 1. Schema Design
38
39```javascript
40// Embed when:
41// - One-to-few relationship
42// - Data doesn't change often
43// - Need atomic updates
44
45// Reference when:
46// - One-to-many or many-to-many
47// - Data changes frequently
48// - Documents would exceed 16MB
49```
50
51### 2. Indexing
52
53```javascript
54// Index fields used in:
55// - Queries ($match, find)
56// - Sorts ($sort)
57// - Joins ($lookup)
58
59// Avoid:
60// - Too many indexes (slows writes)
61// - Indexes on fields with low cardinality
62```
63
64### 3. Aggregation
65
66```javascript
67// Put $match early in pipeline
68// Use $limit after $sort
69// Use indexes with $match and $sort
70```
71
72### 4. Sharding
73
74```javascript
75// Choose shard key carefully
76// High cardinality
77// Good distribution
78// Query isolation
79```
80
81### 5. Connection Pooling
82
83```javascript
84// Use connection pools
85// Don't create new connections for each operation
86const client = new MongoClient(uri, {
87 maxPoolSize: 10,
88 minPoolSize: 2,
89});
90```
91
92## Approach
93
94When working with MongoDB:
95
961. **Design Schema**: Consider access patterns first
972. **Index Strategically**: Cover common queries
983. **Use Aggregation**: For complex queries and transformations
994. **Monitor Performance**: Enable profiling, use explain
1005. **Use Replication**: High availability and read scaling
1016. **Shard When Needed**: For horizontal scaling
1027. **Backup Regularly**: mongodump or filesystem snapshots
1038. **Security**: Authentication, encryption, network isolation
104
105Always design MongoDB databases that are performant, scalable, and maintainable.
106
107## Reference Documentation
108
109Detailed material lives alongside this skill and is read on demand:
110
111- [Core Expertise](references/CORE_CONCEPTS.md) — CRUD Operations, Query Operators, Aggregation Pipeline, Indexing, Schema Design, Transactions, Replication, Performance Optimization