MongoDB Requirements
Ceboelha API uses MongoDB as its database, accessed through Mongoose ODM. Minimum version: MongoDB 6.0 or higherInstallation Options
- Local MongoDB
- Docker
- MongoDB Atlas
Install MongoDB locally on your machine:macOS (Homebrew):Ubuntu/Debian:Windows:
Download and install from MongoDB Download Center
Connection Configuration
Configure theMONGODB_URI environment variable in your .env file based on your setup:
Local MongoDB (No Authentication)
Local MongoDB (With Authentication)
MongoDB Atlas
user, password, and cluster with your actual credentials and cluster name.
Database Connection
Ceboelha API handles database connection automatically on startup using Mongoose. The connection includes:- Automatic reconnection on disconnection
- Connection monitoring with event logging
- Graceful shutdown handling
Connection Events
The API logs various connection events:Testing the Connection
Verify your MongoDB connection before starting the application:- Attempt to connect to MongoDB using your
MONGODB_URI - Report the connection status
- Display the connected host
- Exit gracefully
Database Seeding
Ceboelha API provides scripts to populate your database with initial data.Seed All Data
- Sample users
- Initial configuration
- Test data for development
Seed FODMAP Foods
- Food names and categories
- FODMAP levels (high, medium, low)
- Serving sizes
- Nutritional information
Seeding is optional and primarily useful for development and testing. Production databases typically start empty and are populated through the API.
Data Models
Ceboelha API uses Mongoose schemas for data modeling. The main collections are:| Collection | Description |
|---|---|
users | User accounts and authentication |
symptoms | IBS symptom tracking entries |
foods | FODMAP food database |
meals | User meal logs |
diaries | Daily health diaries |
Schema Validation
All models include:- Type validation - Ensures data types are correct
- Required fields - Enforces mandatory fields
- Custom validators - Business logic validation
- Timestamps - Automatic
createdAtandupdatedAtfields
Database Connection Flow
The application handles database connection in the following sequence:Setup event handlers
Register event handlers for connection monitoring:
error- Log connection errorsdisconnected- Log disconnection warningsreconnected- Log successful reconnections
Graceful Shutdown
When the application receives a shutdown signal (SIGTERM, SIGINT), it:- Stops accepting new HTTP requests
- Waits for in-flight requests to complete
- Closes the database connection gracefully
- Exits the process
Troubleshooting
Connection Refused
Error:- Verify MongoDB is running:
mongoshor check service status - Check the port (default: 27017)
- Verify firewall settings
Authentication Failed
Error:- Verify username and password in
MONGODB_URI - Check
authSourceparameter (usuallyadmin) - Ensure user has appropriate permissions
Network Timeout
Error:- Check network connectivity
- Verify MongoDB Atlas IP whitelist
- Check if MongoDB is accessible from your network
Production Considerations
- Use authentication - Never run MongoDB without authentication in production
- Enable SSL/TLS - Encrypt database connections
- Whitelist IPs - Only allow connections from your application servers
- Regular backups - Set up automated database backups
- Monitor performance - Use MongoDB Atlas monitoring or similar tools
- Set replica sets - Use replica sets for high availability
- Resource limits - Configure appropriate memory and connection limits
Next Steps
Authentication
Learn how to authenticate and manage users
API Reference
Explore the API endpoints and data models