Skip to main content
The application uses environment variables to configure MongoDB connections and other runtime settings.

Required Variables

MONGODB_URI

Required: Yes
Type: String
Description: MongoDB connection string
The connection string for your MongoDB database. Supports both standard and SRV formats.
# MongoDB Atlas (recommended for production)
MONGODB_URI=mongodb+srv://username:[email protected]/?retryWrites=true&w=majority

# Local MongoDB
MONGODB_URI=mongodb://localhost:27017

# MongoDB with authentication
MONGODB_URI=mongodb://username:password@host:27017/database?authSource=admin
The application will throw an error if MONGODB_URI is not defined: MONGODB_URI no está definida en .env

MONGODB_DB

Required: No
Type: String
Default: portafolio
Description: Database name to use in MongoDB
Specifies which database to query for NFQ events.
MONGODB_DB=portafolio

MONGODB_COLLECTION_NFQ

Required: No
Type: String
Default: nfq
Description: Collection name for NFQ events
Specifies which collection contains the NFQ (No Failure Questions) events.
MONGODB_COLLECTION_NFQ=nfq

Configuration by Environment

Local Development

Create a .env file in the project root:
# .env
MONGODB_URI=mongodb://localhost:27017
MONGODB_DB=portafolio
MONGODB_COLLECTION_NFQ=nfq
The application uses dotenv to load these variables:
import * as dotenv from "dotenv";
import { resolve } from "path";

dotenv.config({ path: resolve(process.cwd(), ".env"), override: false });
The override: false option means environment variables already set in process.env take precedence over the .env file.

Docker Deployment

Pass environment variables when running the container:
docker run -p 3000:3000 \
  -e MONGODB_URI="mongodb+srv://user:[email protected]/?retryWrites=true&w=majority" \
  -e MONGODB_DB="portafolio" \
  -e MONGODB_COLLECTION_NFQ="nfq" \
  portfolio-javier-navas

Cloud Platforms

Each cloud platform has its own method for setting environment variables:
# Task Definition JSON
{
  "containerDefinitions": [{
    "environment": [
      {
        "name": "MONGODB_URI",
        "value": "mongodb+srv://user:[email protected]"
      },
      {
        "name": "MONGODB_DB",
        "value": "portafolio"
      },
      {
        "name": "MONGODB_COLLECTION_NFQ",
        "value": "nfq"
      }
    ]
  }]
}

Security Best Practices

Never commit .env files or environment variables containing secrets to version control.

Protecting Credentials

  1. Add to .gitignore
# .gitignore
.env
.env.local
.env.*.local
  1. Use secrets management for production:
    • AWS Secrets Manager
    • Google Secret Manager
    • Azure Key Vault
    • HashiCorp Vault
  2. URL-encode special characters in connection strings:
# If password is p@ssw0rd!
# Encode as: p%40ssw0rd%21
MONGODB_URI=mongodb+srv://user:p%40ssw0rd%[email protected]
  1. Rotate credentials regularly and update environment variables
  2. Use read-only credentials when the application doesn’t need write access

Environment-Specific Variables

Maintain separate configurations for each environment:
# .env.development
MONGODB_URI=mongodb://localhost:27017
MONGODB_DB=portafolio_dev

# .env.staging
MONGODB_URI=mongodb+srv://staging:[email protected]
MONGODB_DB=portafolio_staging

# .env.production
MONGODB_URI=mongodb+srv://prod:[email protected]
MONGODB_DB=portafolio

Validation

The application validates that MONGODB_URI is set at runtime:
const uri = process.env.MONGODB_URI;
if (!uri) throw new Error("MONGODB_URI no está definida en .env");
If the variable is missing, the application will crash on startup with a clear error message.

Example .env File

Here’s a complete example .env file:
# MongoDB Configuration
MONGODB_URI=mongodb+srv://portfolio_user:[email protected]/?retryWrites=true&w=majority
MONGODB_DB=portafolio
MONGODB_COLLECTION_NFQ=nfq

# Optional: Node environment
NODE_ENV=production

Troubleshooting

Variable Not Loading

  1. Verify .env file is in the project root
  2. Check file has no syntax errors (no spaces around =)
  3. Restart the development server after changes
  4. Ensure the file is not named .env.txt or similar

Connection String Issues

  1. Special characters: URL-encode special characters in passwords
  2. Whitespace: Remove any trailing spaces from values
  3. Quotes: Don’t use quotes around values in .env files
# Correct
MONGODB_URI=mongodb://localhost:27017

# Incorrect
MONGODB_URI="mongodb://localhost:27017"
MONGODB_URI = mongodb://localhost:27017

Production vs Development

If variables work locally but not in production:
  1. Verify environment variables are set in your deployment platform
  2. Check that the platform restarts the application after setting variables
  3. Review platform-specific logs for configuration errors
  4. Ensure secrets are not masked or truncated

Next Steps

MongoDB Setup

Configure MongoDB database and collections

Docker Deployment

Deploy using Docker containers

Build docs developers (and LLMs) love