MongoDB Cheat Sheet
Practical MongoDB cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.
mongodb queries schema performance
MongoDB cheat sheet with real commands and snippets for setup, core workflows, debugging, and production-safe automation patterns. If you are working across tools, pair this with the GraphQL Cheat Sheet and Redis Cheat Sheet.
Setup and Connection
Goal: Connect to database shell or endpoint
# Run connection or version command
mongosh
# Run connection or version command
mongosh "mongodb://localhost:27017/app"
# Run connection or version command
mongosh --version
Schema and Data Modeling
Goal: Create a table/index/schema foundation
// Insert sample document with nested fields
db.users.insertOne({
email: 'user@example.com',
profile: { name: 'Rem', role: 'admin' },
createdAt: new Date()
});
Core Queries and Mutations
Goal: Run practical read/write operations
// Query by nested field and project selected keys
db.users.find(
{ 'profile.role': 'admin' },
{ email: 1, 'profile.name': 1, _id: 0 }
);
Performance and Optimization
Goal: Add indexes and inspect query plans
// Create index and inspect usage plan
db.users.createIndex({ email: 1 }, { unique: true });
db.users.find({ email: 'user@example.com' }).explain('executionStats');
Automation and Backups
Goal: Create backup and restore routines
# Export data snapshot before risky migrations
pg_dump "$DATABASE_URL" > backup.sql || mysqldump appdb > backup.sql || true
# Compress backup artifact for storage
gzip -f backup.sql || true
# Verify backup file exists and has content
ls -lh backup.sql.gz || ls -lh backup.sql
Debugging and Troubleshooting
Goal: Inspect slow-query symptoms
# Check active connections/sessions
psql -c "SELECT now();" || mysql -e "SELECT NOW();" || true
# Inspect query plans for heavy statements
psql -c "EXPLAIN SELECT 1;" || mysql -e "EXPLAIN SELECT 1;" || true
# Search migration files for destructive statements
rg "DROP TABLE|TRUNCATE|DELETE FROM" prisma migrations sql -n
Common Gotchas
- Use transactions for multi-step MongoDB data changes that must stay consistent.
- Create indexes for frequent WHERE and JOIN columns, then verify with query plans.
- Back up production data before schema migrations.
- Separate read-only credentials from write credentials in app environments.
- Monitor connection pool limits to avoid timeout spikes under load.
Related Sheets
- GraphQL Cheat Sheet — daily GraphQL commands and production-ready examples.
- Redis Cheat Sheet — cache and queue operations.
- Elasticsearch Cheat Sheet — daily Elasticsearch commands and production-ready examples.
Related Cheat Sheets
Elasticsearch Cheat Sheet
Practical Elasticsearch cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.
GraphQL Cheat Sheet
Practical GraphQL cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.
Redis Cheat Sheet
Practical Redis cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.