- Node.js installed (v14 or higher)
- MongoDB Atlas account (free) OR Docker installed
cd mongodb/
npm installChoose ONE option:
Option A: MongoDB Atlas (Recommended - No installation needed)
- Go to MongoDB Atlas
- Create a free account and cluster
- Click "Connect" → "Connect your application"
- Copy the connection string
- Create
.envfile:
MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/hackathon?retryWrites=true&w=majority
PORT=3000Option B: Local Docker
# Start MongoDB container
docker run -d -p 27017:27017 --name mongodb-hackathon mongo:latest
# Create .env file
echo "MONGODB_URI=mongodb://localhost:27017/hackathon" > .env
echo "PORT=3000" >> .envnpm run test-connectionYou should see:
MongoDB Connected Successfully!
Database: hackathon
Connection String: mongodb://...
npm run seednpm startServer runs at: http://localhost:3000
mongodb/
├── package.json # Dependencies
├── .env.example # Environment template
├── .env # Your config (create this!)
├── .gitignore # Git ignore file
├── MongoDB_Starter.md # This file
├── src/
│ ├── server.js # Express server setup
│ ├── config/
│ │ └── database.js # MongoDB connection
│ ├── models/ # Mongoose schemas
│ │ ├── User.js
│ │ └── Project.js
│ ├── routes/ # API endpoints
│ │ ├── users.js
│ │ └── projects.js
│ ├── controllers/ # Business logic
│ │ ├── userController.js
│ │ └── projectController.js
│ └── middleware/ # Custom middleware
│ └── errorHandler.js
├── scripts/
│ └── seed.js # Sample data seeder
└── tests/
├── testConnection.js # Connection test
└── api.test.js # Basic API tests
Once running, test these endpoints:
GET /api/users- Get all usersGET /api/users/:id- Get user by IDPOST /api/users- Create new userPUT /api/users/:id- Update userDELETE /api/users/:id- Delete user
GET /api/projects- Get all projectsGET /api/projects/:id- Get project by IDGET /api/projects/user/:userId- Get projects by userPOST /api/projects- Create new projectPUT /api/projects/:id- Update projectDELETE /api/projects/:id- Delete project
curl -X POST http://localhost:3000/api/users \
-H "Content-Type: application/json" \
-d '{
"username": "johndoe",
"email": "john@example.com",
"fullName": "John Doe"
}'curl -X POST http://localhost:3000/api/projects \
-H "Content-Type: application/json" \
-d '{
"title": "My Hackathon Project",
"description": "Building something awesome!",
"userId": "USER_ID_FROM_ABOVE",
"technologies": ["mongodb", "node.js", "react"]
}'curl http://localhost:3000/api/projects- Create model file in
src/models/:
// src/models/Team.js
const mongoose = require('mongoose');
const teamSchema = new mongoose.Schema({
name: { type: String, required: true },
members: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
createdAt: { type: Date, default: Date.now }
});
module.exports = mongoose.model('Team', teamSchema);- Create routes file in
src/routes/:
// src/routes/teams.js
const express = require('express');
const router = express.Router();
const Team = require('../models/Team');
router.get('/', async (req, res) => {
const teams = await Team.find().populate('members');
res.json(teams);
});
module.exports = router;- Register routes in
src/server.js:
const teamRoutes = require('./routes/teams');
app.use('/api/teams', teamRoutes);# Test your connection
npm run test-connection
# Check your .env file
cat .envCommon issues:
- Check your
.envfile exists and has correct MongoDB URI - Verify MongoDB Atlas IP whitelist (set to 0.0.0.0/0 for development)
- Ensure MongoDB service is running (if using Docker/local)
- Check if username/password contains special characters (URL encode them)
# Change PORT in .env file to another port (e.g., 3001)
PORT=3001# Clear database and re-seed
npm run seed:fresh# Install Heroku CLI, then:
heroku create your-app-name
heroku config:set MONGODB_URI=your_atlas_connection_string
git push heroku main- Use MongoDB Atlas for database
- Set environment variables in platform settings