Files
grateful-journal/docs/MONGODB_SETUP.md
2026-03-04 12:23:13 +05:30

4.3 KiB

MongoDB Setup Guide for Grateful Journal

Prerequisites

  • MongoDB installed on your system
  • Python 3.9+
  • pip package manager

Installation Steps

1. Install MongoDB

macOS (using Homebrew)

brew tap mongodb/brew
brew install mongodb-community

Linux (Ubuntu/Debian)

curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org

Windows

Download and run the installer from MongoDB Community Download

2. Start MongoDB Server

macOS / Linux

# Start as a service (recommended)
brew services start mongodb-community

# Or run directly
mongod --config /usr/local/etc/mongod.conf

Windows

MongoDB should run as a service. If not:

net start MongoDB

3. Verify MongoDB is Running

mongosh

If you see a connection prompt, MongoDB is running successfully.

4. Set up Python Backend Environment

Navigate to the backend directory:

cd backend

Create a virtual environment:

python3 -m venv venv
source venv/bin/activate  # macOS/Linux
# or
venv\Scripts\activate  # Windows

Install dependencies:

pip install -r requirements.txt

5. Configure Environment Variables

Copy the example env file:

cp .env.example .env

Edit .env with your settings (defaults work for local development):

MONGODB_URI=mongodb://localhost:27017
MONGODB_DB_NAME=grateful_journal
API_PORT=8001
ENVIRONMENT=development
FRONTEND_URL=http://localhost:8000

6. Run the FastAPI Server

python main.py

You should see:

✓ Connected to MongoDB: grateful_journal
INFO:     Uvicorn running on http://0.0.0.0:8001

7. Access API Documentation

Open your browser and go to:

MongoDB Collections Overview

The following collections will be created automatically on first write:

users

Stores user profiles after Firebase Google Auth.

{
  "_id": ObjectId,
  "email": "user@example.com",
  "displayName": "John Doe",
  "photoURL": "https://...",
  "theme": "light",
  "createdAt": ISODate,
  "updatedAt": ISODate
}

entries

Stores journal entries.

{
  "_id": ObjectId,
  "userId": "user_id_string",
  "title": "Today's thoughts",
  "content": "Long-form journal content...",
  "mood": "grateful",
  "tags": ["reflection", "gratitude"],
  "isPublic": false,
  "createdAt": ISODate,
  "updatedAt": ISODate
}

settings

Stores user preferences and settings.

{
  "_id": ObjectId,
  "userId": "user_id_string",
  "notifications": true,
  "emailNotifications": false,
  "theme": "light",
  "language": "en",
  "updatedAt": ISODate
}

API Endpoints

Users

  • POST /api/users/register — Register user after Firebase auth
  • GET /api/users/by-email/{email} — Get user profile by email
  • PUT /api/users/update/{user_id} — Update user profile
  • DELETE /api/users/{user_id} — Delete user and associated data

Entries

  • POST /api/entries/{user_id} — Create new entry
  • GET /api/entries/{user_id} — Get all entries (paginated)
  • GET /api/entries/{user_id}/{entry_id} — Get specific entry
  • PUT /api/entries/{user_id}/{entry_id} — Update entry
  • DELETE /api/entries/{user_id}/{entry_id} — Delete entry
  • GET /api/entries/{user_id}/date/{date_str} — Get entries by date (YYYY-MM-DD)

Troubleshooting

MongoDB connection refused

  • Check that MongoDB service is running: brew services list (macOS)
  • Verify port 27017 is not blocked

ModuleNotFoundError: pymongo

  • Ensure virtual environment is activated
  • Run pip install -r requirements.txt again

CORS errors in frontend

Next Steps

Once MongoDB and FastAPI are running:

  1. Frontend calls Firebase Google Auth
  2. Frontend sends auth token to /api/users/register
  3. Backend creates user in MongoDB
  4. Frontend can now call /api/entries/* endpoints with user_id