AI Skill Report Card
Working with MongoDB
MongoDB Development
Quick Start15 / 15
JavaScript// Connect and perform basic operations const { MongoClient } = require('mongodb'); const client = new MongoClient('mongodb://localhost:27017'); await client.connect(); const db = client.db('myapp'); const collection = db.collection('users'); // Insert document await collection.insertOne({ name: 'John Doe', email: 'john@example.com', createdAt: new Date() }); // Query with index-friendly patterns const users = await collection.find({ email: 'john@example.com' }).toArray();
Recommendation▾
Add concrete templates for schema design patterns (one-to-one, one-to-many, many-to-many) with specific document structures
Workflow13 / 15
Progress:
- Schema Design: Plan document structure and relationships
- Indexing Strategy: Create appropriate indexes before data growth
- Query Implementation: Write efficient queries using aggregation pipeline
- Performance Testing: Monitor query performance and optimize
- Error Handling: Implement proper connection and operation error handling
Schema Design Process
- Identify access patterns and query requirements
- Design embedded vs referenced data based on read/write patterns
- Plan for data growth and sharding if needed
- Validate schema with sample data
Query Optimization
- Use
explain()to analyze query execution - Create compound indexes for multi-field queries
- Use aggregation pipeline for complex data transformations
- Implement proper pagination with cursor-based approach
Recommendation▾
Include performance monitoring examples with specific metrics and thresholds (e.g., query execution time > 100ms)
Examples18 / 20
Example 1: User Profile with Embedded Data Input: User with posts and comments
JavaScript// Schema design { _id: ObjectId, username: "johndoe", profile: { firstName: "John", lastName: "Doe", avatar: "avatar.jpg" }, recentPosts: [ { title: "Post 1", createdAt: ISODate, excerpt: "..." } ] } // Efficient query db.users.find({ username: "johndoe" }).project({ "recentPosts": { $slice: 5 } })
Example 2: Aggregation Pipeline Input: Calculate user engagement metrics
JavaScriptdb.users.aggregate([ { $match: { lastLogin: { $gte: new Date(Date.now() - 30*24*60*60*1000) } } }, { $lookup: { from: "posts", localField: "_id", foreignField: "authorId", as: "posts" } }, { $project: { username: 1, postCount: { $size: "$posts" }, avgLikes: { $avg: "$posts.likes" } }}, { $sort: { postCount: -1 } } ])
Example 3: Proper Indexing
JavaScript// Compound index for common query patterns db.users.createIndex({ "status": 1, "createdAt": -1 }) db.posts.createIndex({ "authorId": 1, "publishedAt": -1 }) // Text index for search db.posts.createIndex({ "title": "text", "content": "text" })
Recommendation▾
Provide connection configuration templates for different environments (development, staging, production) with appropriate settings
Best Practices
- Always use connection pooling in production environments
- Create indexes before inserting large datasets to avoid performance issues
- Use projection to limit returned fields and reduce network traffic
- Implement proper error handling with retry logic for transient failures
- Use transactions for multi-document operations that need consistency
- Validate data at application level before database operations
- Monitor slow queries using MongoDB profiler and explain plans
- Use appropriate read/write concerns based on consistency requirements
Common Pitfalls
- Don't create indexes on every field - they slow down writes and consume space
- Avoid large arrays (>100 elements) in frequently updated documents
- Don't use regex queries without proper indexes (causes collection scans)
- Never store sensitive data without encryption
- Don't ignore connection limits - always close connections properly
- Avoid deeply nested documents (MongoDB limit is 100 levels)
- Don't use
findAndModifyfor bulk operations - use bulk operations instead - Never perform joins in application code - use
$lookupaggregation stage