Overview
BillBuddy uses MongoDB as its primary database, with Mongoose as the ODM (Object Data Modeling) library. This guide covers MongoDB setup, configuration, and connection management.Prerequisites
MongoDB version 4.4 or higher is required
Node.js version 14 or higher is required
MongoDB Installation
Local Installation
macOS (using Homebrew):Verify Installation
Connection Configuration
Environment Variable
Configure the MongoDB connection using theMONGODB_URI environment variable in your .env file:
Complete MongoDB connection string including protocol, host, port, and database name.
Local Development
For local development, use the default MongoDB connection:.env
mongodb://- MongoDB protocollocalhost:27017- Host and port (default MongoDB port)billbuddy- Database name
MongoDB Atlas (Cloud)
For cloud deployment using MongoDB Atlas:.env
Replace
<username>, <password>, and <cluster> with your actual Atlas credentials and cluster name.- Create an account at MongoDB Atlas
- Create a new cluster (free tier available)
- Add a database user with read/write permissions
- Whitelist your IP address or allow access from anywhere (0.0.0.0/0)
- Get the connection string from the “Connect” button
Server Connection Code
The MongoDB connection is established inserver.js:
backend/server.js
The connection uses Mongoose’s default options, which include automatic reconnection and connection pooling.
Connection Options
While BillBuddy uses default connection options, you can customize the connection with additional parameters:Database Schema
BillBuddy uses four main collections:Users Collection
Stores user account information:backend/models/User.js
Groups Collection
Stores expense groups and members.Expenses Collection
Stores individual expense records with splits.Settlements Collection
Stores settlement history for groups.Database Operations
Creating Indexes
BillBuddy automatically creates indexes based on schema definitions:emailfield in User model has a unique index- All models have indexes on their
_idfield (MongoDB default)
Database Queries
Example queries used in BillBuddy:Connection Management
Connection Events
Monitor MongoDB connection status:Graceful Shutdown
Close database connections on application shutdown:Testing the Connection
Verify your database connection:Database Management
Using MongoDB Compass
MongoDB Compass provides a GUI for database management:- Download MongoDB Compass
- Connect using your connection string
- Browse collections, documents, and indexes
- Run queries and aggregations
Using MongoDB Shell
Connect to your database via command line:Troubleshooting
Connection Refused
Problem:MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
Solution:
- Verify MongoDB is running:
brew services list(macOS) orsudo systemctl status mongod(Linux) - Start MongoDB if stopped:
brew services start mongodb-communityorsudo systemctl start mongod
Authentication Failed
Problem:MongoServerError: Authentication failed
Solution:
- Check username and password in connection string
- Verify database user has correct permissions
- For Atlas, ensure IP whitelist is configured
Database Not Found
MongoDB automatically creates the database when you first insert data. You don’t need to manually create it.
Connection Timeout
Problem:MongoServerSelectionError: connection timed out
Solution:
- Check network connectivity
- Verify firewall settings
- For Atlas, check IP whitelist
- Increase
serverSelectionTimeoutMSin connection options
Performance Optimization
Connection Pooling
Mongoose manages a connection pool automatically. Adjust pool size for high-traffic applications:Query Optimization
- Use
select()to limit returned fields - Add indexes for frequently queried fields
- Use
lean()for read-only queries - Implement pagination for large datasets
Monitoring
Monitor database performance:- MongoDB Atlas provides built-in monitoring
- Use MongoDB Compass performance tab
- Enable Mongoose debug mode:
mongoose.set('debug', true)