MongoDB Database
Table of contents
- MongoDB Configuration
MongoDB Configuration
This directory contains the MongoDB configuration and seed data for the MEAN Stack Contacts application.
Initialization Script
The main initialization script (init-mongo.sh) performs two primary tasks:
- Creates a database user with appropriate permissions
- Seeds the database with initial data (users and contacts)
User Creation
mongosh $MONGO_DB -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD --eval "db.createUser({ user: '$MONGO_DB_USERNAME', pwd: '$MONGO_DB_PASSWORD', roles: [{ role: 'readWrite', db: '$MONGO_DB'}]})" --authenticationDatabase admin
Data Seeding
The script seeds the database with initial data:
- One user account with login credentials
- Three sample contacts
db.users.insertMany([
{
"_id": ObjectId("6759daeb097c7d69426b7649"),
"create_date": new Date("2024-12-11T19:29:19.895Z"),
"username": "nitin27may@gmail.com",
"email": "nitin27may@gmail.com",
"password": "$2a$10$nc0yO2eeCDOLg6sObsAHfuXQY8NnCrhHz5GbkmPYsAGLsQoSZa.qm",
"firstName": "NItin",
"lastName": "Singh"
}
]);
db.contacts.insertMany([
{
"_id": ObjectId("6759daeb097c7d69426b7641"),
"firstName": "Nitin",
"lastName": "Singh",
"mobile": "9876543243",
"email": "nitin27may@gmail.com",
"city": "Mumbai",
"postalCode": "421201",
"create_date": new Date()
},
// Additional contacts...
]);
Default User Credentials
The seed data creates a default user with the following credentials:
Username: nitin27may@gmail.com
Password: P@ssword#321
Environment Variables
The MongoDB container uses the following environment variables:
MONGO_INITDB_ROOT_USERNAME: Root username for MongoDBMONGO_INITDB_ROOT_PASSWORD: Root password for MongoDBMONGO_DB_USERNAME: Username for the application databaseMONGO_DB_PASSWORD: Password for the application databaseMONGO_DB: Database name
These variables should be defined in the root .env file of the project.
Docker Configuration
The MongoDB container uses the official MongoDB image with custom initialization:
FROM mongo
COPY init-db.d /docker-entrypoint-initdb.d
Volumes
The Docker Compose configuration maps the following volumes:
./mongo/init-db.d/:/docker-entrypoint-initdb.d/: Initialization scriptsmongo-data:/data/db: data persistence, in a named volume
Data Persistence
MongoDB data is persisted in the mongo-data named volume, so it survives docker compose down and container restarts. A named volume is used rather than a bind mount so the database does not leave root-owned files in your working tree.
Connecting to MongoDB
From Docker Containers
Other containers can connect to MongoDB using the following connection string:
mongodb://${MONGO_DB_USERNAME}:${MONGO_DB_PASSWORD}@database:27017/${MONGO_DB_DATABASE}?authSource=admin
From the Host Machine
Only in the development mode (docker compose up), which publishes 27017. The Nginx modes deliberately do not — MongoDB is reachable only on the internal network there.
mongodb://${MONGO_DB_USERNAME}:${MONGO_DB_PASSWORD}@localhost:27017/${MONGO_DB_DATABASE}?authSource=admin
Using MongoDB Compass or Robo 3T
To connect using GUI tools like MongoDB Compass or Robo 3T:
- Host:
localhost - Port:
27017 - Authentication:
Username/Password - Username: Value of
MONGO_DB_USERNAMEfrom your.envfile - Password: Value of
MONGO_DB_PASSWORDfrom your.envfile - Authentication Database:
admin - Database: Value of
MONGO_DB_DATABASEfrom your.envfile
Troubleshooting
Resetting the seed data
mongo/init-db.d/init-mongo.sh runs only against an empty data volume, so editing it has no effect on a database that already exists. Drop the volume and bring the stack back up:
docker compose -f docker-compose.nginx.yml down -v
docker compose -f docker-compose.nginx.yml up --build
Connection Issues
If you cannot connect to MongoDB, check:
- The MongoDB container is running:
docker ps - Environment variables are set correctly in
.env - Ports are not blocked by a firewall
- No other process is using port 27017
Initialization Script Issues
If the initialization script fails, check:
- The script has proper permissions:
chmod +x ./mongo/init-db.d/init-mongo.sh - Line endings are set to LF:
git config --global core.autocrlf input - No syntax errors in the script
Customizing Seed Data
To modify the seed data:
- Edit the
init-mongo.shfile in theinit-db.ddirectory - Rebuild and restart the containers:
docker compose down docker compose -f docker-compose.nginx.yml up --build