# Install MongoDB Community Edition
brew tap mongodb/brew
brew install mongodb-community
# Start MongoDB as a service (runs in background)
brew services start mongodb-community
# Verify it's running
brew services list | grep mongodb
# Test connection
mongosh# Start MongoDB manually (stays in foreground)
mongod --config /opt/homebrew/etc/mongod.conf
# Or with custom data directory
mongod --dbpath ~/data/db# Import MongoDB public GPG key
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
# Add MongoDB repository
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
# Update and install
sudo apt-get update
sudo apt-get install -y mongodb-org
# Start MongoDB
sudo systemctl start mongod
# Enable auto-start on boot
sudo systemctl enable mongod
# Check status
sudo systemctl status mongod# Pull MongoDB image
docker pull mongo:latest
# Run MongoDB container
docker run -d \
--name mongodb-test \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password \
mongo:latest
# Check it's running
docker ps | grep mongodb-test
# Connect
mongosh "mongodb://admin:password@localhost:27017"Create docker-compose.yml in your project root:
version: '3.8'
services:
mongodb:
image: mongo:latest
container_name: kindred-mongodb-test
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: password
volumes:
- mongodb_data:/data/db
restart: unless-stopped
volumes:
mongodb_data:Then run:
# Start MongoDB
docker-compose up -d
# Stop MongoDB
docker-compose down
# Stop and remove data
docker-compose down -vUse the built-in verification script:
# From project root
make check-mongodb
# Or directly
./backend/scripts/check-mongodb.shThis will verify:
- ✅ mongosh is installed
- ✅ MongoDB is running and accessible
- ✅ Can create and drop databases
- ✅ No leftover test databases
- ✅ Ready for tests
# Method 1: Check process
ps aux | grep mongod
# Method 2: Check port
lsof -i :27017
# Method 3: Try connecting
mongosh# Connect
mongosh
# Show databases
show dbs
# Create a test database
use test_connection
# Insert a document
db.test.insertOne({ message: "Hello MongoDB!" })
# Query it back
db.test.find()
# Exit
exitIf MongoDB is running on localhost:27017, tests will work automatically:
# Just run your tests
cd backend
go test ./...If using Docker with authentication or different port:
# Set environment variable
export TEST_MONGO_URI="mongodb://admin:password@localhost:27017"
# Run tests
cd backend
go test ./...# Use your Atlas connection string
export TEST_MONGO_URI="mongodb+srv://username:password@cluster.mongodb.net/"
# Run tests
cd backend
go test ./...Cause: MongoDB not running
Solution:
# macOS
brew services start mongodb-community
# Linux
sudo systemctl start mongod
# Docker
docker start mongodb-testCause: Wrong credentials or auth not configured
Solution:
# For local MongoDB without auth (default)
export TEST_MONGO_URI="mongodb://localhost:27017"
# For Docker with auth
export TEST_MONGO_URI="mongodb://admin:password@localhost:27017"Cause: Another process using the port
Solution:
# Find what's using the port
lsof -i :27017
# Kill the process (use PID from above)
kill -9 <PID>
# Or use a different port
mongod --port 27018
export TEST_MONGO_URI="mongodb://localhost:27018"Cause: MongoDB can't write to data directory
Solution:
# Create data directory with correct permissions
sudo mkdir -p /data/db
sudo chown -R $(whoami) /data/db
# Or use a custom directory
mkdir -p ~/mongodb-data
mongod --dbpath ~/mongodb-dataPros:
- ✅ Isolated from system
- ✅ Easy to start/stop
- ✅ Consistent across team
- ✅ Easy to reset
Setup:
# Add docker-compose.yml to project root (see above)
docker-compose up -d
# Tests work automatically
cd backend
go test ./...Pros:
- ✅ Runs in background
- ✅ Auto-starts on boot
- ✅ Native performance
Setup:
brew services start mongodb-community
# Tests work automatically
cd backend
go test ./...Pros:
- ✅ No local setup needed
- ✅ Same as production
- ✅ Accessible from anywhere
Cons:
- ❌ Requires internet
- ❌ Slower than local
- ❌ May have rate limits
Setup:
export TEST_MONGO_URI="mongodb+srv://..."
cd backend
go test ./...-
Use Local MongoDB (not Atlas)
- Network latency matters for tests
- Local is 10-100x faster
-
Use SSD for Data Directory
- Faster disk = faster tests
- Default locations are usually on SSD
-
Disable Journaling for Tests (optional)
mongod --nojournal --dbpath ~/test-data -
Use In-Memory Storage (fastest)
mongod --storageEngine inMemory --dbpath /tmp/mongo-mem
# Connect to MongoDB
mongosh
# List all test databases
show dbs
# Drop test databases (one by one)
use test_1706234567890123456
db.dropDatabase()
# Or use this script to drop all test_* databases
mongosh --eval '
db.adminCommand("listDatabases").databases
.filter(d => d.name.startsWith("test_"))
.forEach(d => {
print("Dropping: " + d.name);
db.getSiblingDB(d.name).dropDatabase();
})
'# Homebrew
brew services stop mongodb-community
# Linux
sudo systemctl stop mongod
# Docker
docker stop mongodb-test
# or
docker-compose down# Homebrew
brew services stop mongodb-community
brew uninstall mongodb-community
rm -rf /opt/homebrew/var/mongodb
# Linux
sudo systemctl stop mongod
sudo apt-get purge mongodb-org*
sudo rm -rf /var/log/mongodb
sudo rm -rf /var/lib/mongodb
# Docker
docker stop mongodb-test
docker rm mongodb-test
docker rmi mongoname: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
services:
mongodb:
image: mongo:latest
ports:
- 27017:27017
options: >-
--health-cmd "mongosh --eval 'db.adminCommand(\"ping\")'"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.25.6'
- name: Run tests
run: |
cd backend
go test ./...
env:
TEST_MONGO_URI: mongodb://localhost:27017Recommended for most developers:
# Install
brew install mongodb-community
# Start
brew services start mongodb-community
# Run tests
cd backend
go test ./...
# That's it! 🎉The test infrastructure will automatically:
- Create ephemeral databases for each test
- Seed them with fixtures
- Drop them when tests complete
- Work with your local MongoDB instance