Skip to content

Add MONGO_URI Environment Variable Support #253

Closed

Description

@anuragxxd

Current Limitation

FOCA's MongoDB registration in foca/database/register_mongodb.py currently only supports individual environment variables (MONGO_HOST, MONGO_PORT, MONGO_USERNAME, MONGO_PASSWORD, MONGO_DBNAME) and constructs the MongoDB URI internally. This prevents users from providing complete MongoDB connection strings with advanced features like:

  • Replica sets with multiple hosts
  • SRV records
  • Custom connection options (SSL, read preferences, etc.)
  • Pre-constructed URIs from external secret management

Requested Enhancement

Add support for a MONGO_URI environment variable that, when present, takes precedence over individual component variables while maintaining full backwards compatibility.

Activity

  1. minhpham1810 commented on Mar 16, 2026

    @minhpham1810

    Hi! I’d be happy to work on this issue if it’s still available.

    From reading the current MongoDB registration logic in foca/database/register_mongodb.py, FOCA currently constructs the MongoDB connection string internally using the following environment variables:

    • MONGO_HOST
    • MONGO_PORT
    • MONGO_USERNAME
    • MONGO_PASSWORD
    • MONGO_DBNAME

    My proposed implementation approach is:

    1. Add support for a MONGO_URI environment variable

    If MONGO_URI is present, the MongoDB client will be initialized directly using this URI instead of constructing one from the individual variables.

    2. Preserve full backward compatibility

    The current configuration using the individual variables will remain unchanged and continue to work when MONGO_URI is not provided.

    3. Define clear precedence rules

    Configuration precedence will be:

    MONGO_URI > individual Mongo variables
    

    Meaning:

    • If MONGO_URI exists → use it directly
    • Otherwise → fall back to the current URI construction logic

    4. Keep the change localized

    I plan to implement the change directly in register_mongodb.py so that the rest of the database initialization logic remains unchanged.

    5. Handle advanced Mongo connection scenarios

    Using MONGO_URI will allow deployments to support features that are currently difficult or impossible with the existing configuration:

    • replica sets with multiple hosts
    • SRV records (mongodb+srv)
    • advanced connection options (TLS, read preferences, etc.)
    • URIs injected by secret managers or container platforms

    6. Add/update tests and documentation

    I will add tests (or update existing ones if applicable) to verify:

    • MONGO_URI only configuration
    • legacy variable-based configuration
    • precedence when both configurations are present

    I will also update relevant documentation/examples to mention the new configuration option.

    Please let me know if this approach aligns with the intended behavior before I start implementing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions