What problem does it solve?
This Skill helps developers design, query, and optimize MongoDB-based data models, providing best practices for schemas, efficient queries, and robust aggregation pipelines across MongoDB, Mongoose, NoSQL, and Atlas deployments.
Core Features & Use Cases
- Schema design best practices: Model data for read efficiency, scalability, and maintainable migrations.
- Query & aggregation guidance: Build performant CRUD operations and powerful pipelines for analytics.
- Atlas & deployment guidance: Recommend indexing strategies, sharding considerations, and deployment patterns for MongoDB Atlas.
Quick Start
Install the MongoDB native driver and Mongoose:
- npm install mongodb mongoose
Native Driver example:
- import { MongoClient, ObjectId } from 'mongodb';
- const client = new MongoClient(process.env.MONGODB_URI!);
- const db = client.db('myapp');
- const users = db.collection('users');
- await client.connect();
- await users.insertOne({ name: 'Alice', email: '[email protected]' });
- const user = await users.findOne({ email: '[email protected]' });
- await users.updateOne({ _id: user!._id }, { $set: { name: 'Alice Smith' } });
- await users.deleteOne({ _id: user!._id });
Mongoose setup example:
- import mongoose from 'mongoose';
- await mongoose.connect(process.env.MONGODB_URI!, { maxPoolSize: 10, serverSelectionTimeoutMS: 5000, socketTimeoutMS: 45000 });
- mongoose.connection.on('connected', () => console.log('MongoDB connected'));
- mongoose.connection.on('error', (err) => console.error('MongoDB error:', err));
- mongoose.connection.on('disconnected', () => console.log('MongoDB disconnected'));
Quick Start (continued)
- Graceful shutdown
- process.on('SIGINT', async () => { await mongoose.connection.close(); process.exit(0); });
- // ...additional examples omitted for brevity