Overview
The Money monorepo uses MongoDB as its primary database for the Secure and CashGap applications. This guide covers setting up MongoDB for development and production environments.Database Requirements
MongoDB Version
MongoDB 4.4 or higher
Connection Method
MongoDB URI connection string
ORM/ODM
Mongoose for schema and queries
Authentication
Username/password authentication
Applications Using MongoDB
Secure App
Stores:- User accounts and profiles
- Authentication sessions
- Password vault entries
- Encryption keys and metadata
- OAuth account links
- Email verification tokens
CashGap App
Stores:- User accounts and profiles
- Transaction records
- Financial data
- Authentication sessions
- User preferences
Development Setup
Option 1: Local MongoDB
Option 2: MongoDB Atlas (Cloud)
Recommended for teams and production-like development environments.Create MongoDB Atlas account
Sign up at MongoDB Atlas
Create a cluster
- Click “Build a Database”
- Choose “Shared” (free tier)
- Select your cloud provider and region
- Name your cluster
- Click “Create Cluster”
Configure database access
- Go to “Database Access”
- Click “Add New Database User”
- Create username and strong password
- Grant “Read and write to any database” permission
- Click “Add User”
Configure network access
- Go to “Network Access”
- Click “Add IP Address”
- For development: Click “Allow Access from Anywhere” (0.0.0.0/0)
- For production: Add specific IP addresses
- Click “Confirm”
Get connection string
- Click “Connect” on your cluster
- Choose “Connect your application”
- Copy the connection string
- Replace
<password>with your database password - Replace
<database>with your database name (e.g.,money-dev)
Production Setup
MongoDB Atlas (Recommended)
Create production cluster
Create a separate cluster for production:
- Use a dedicated cluster (M10+) for better performance
- Choose a region close to your deployment
- Enable backups (automatic on paid tiers)
- Consider multi-region deployment for high availability
Security configuration
- Database users: Create dedicated user for production with minimal required permissions
- Network access: Whitelist only your production server IPs
- Encryption: Enable encryption at rest (available on paid tiers)
- TLS/SSL: Always enabled by default on Atlas
Performance optimization
- Indexes: Create indexes on frequently queried fields
- Connection pooling: Configure appropriate pool size
- Read preference: Use appropriate read preference for your use case
- Monitoring: Enable MongoDB Atlas monitoring and alerts
Backup strategy
- Enable continuous backups (available on M10+)
- Configure backup retention policy
- Test restore procedures
- Consider point-in-time recovery
Self-Hosted MongoDB
For self-hosted deployments:Database Schema
The applications use Mongoose for schema definition and queries:Connection Management
Connections are managed per-app:Mongoose Models
Models are defined in each app’smodels directory:
- User models
- Session models
- App-specific data models
Database Operations
Clearing Development Database
A utility script is available to clear development databases:Database Migrations
Currently, the project does not use formal migrations. Schema changes are handled by:- Updating Mongoose models
- Deploying code changes
- Running manual migration scripts if needed
Seeding Data
For development, you can seed test data:Performance Optimization
Indexing
Create indexes on frequently queried fields:Connection Pooling
Configure connection pool size in URI:Query Optimization
- Use
lean()for read-only queries - Select only needed fields
- Use pagination for large result sets
- Avoid N+1 queries with
populate()
Monitoring
MongoDB Atlas Monitoring
Atlas provides built-in monitoring:- Real-time metrics
- Query performance insights
- Index recommendations
- Alerts for issues
Application-Level Monitoring
Log database operations:Backup and Recovery
MongoDB Atlas Backups
- Continuous backups on M10+ clusters
- Point-in-time recovery
- Automated snapshots
- Download backup archives
Manual Backup
Backup Strategy Recommendations
- Automated daily backups
- Retain backups for 30 days minimum
- Test restore procedures monthly
- Store backups in separate location
- Encrypt backup files
Troubleshooting
Connection Refused
Authentication Failed
- Verify username and password are correct
- Check user has permissions on target database
- Ensure authentication is enabled in MongoDB config
- Verify connection string format
Slow Queries
- Enable query profiling:
- Check for missing indexes
- Use
explain()to analyze queries - Review MongoDB Atlas Performance Advisor
Connection Pool Exhausted
- Increase
maxPoolSizein connection string - Check for connection leaks in code
- Ensure connections are properly closed
- Monitor connection usage
Security Best Practices
- Enable authentication: Never run without authentication
- Use strong passwords: Generate random passwords
- Restrict network access: Whitelist specific IPs only
- Enable TLS/SSL: Encrypt data in transit
- Encrypt at rest: Enable encryption for stored data
- Regular updates: Keep MongoDB updated
- Audit logs: Enable and monitor audit logs
- Least privilege: Grant minimum required permissions
- Separate environments: Use different databases for dev/staging/prod
- Regular backups: Maintain backup schedule
Next Steps
Environment Variables
Configure database connection strings
Deployment Overview
Deploy with database configuration
Running Locally
Start development with database
Setup Guide
Complete development setup