Skip to content
archived Visibility internal Owner erik@uvilo.com Approver _ Created _ Updated _

MongoDB

When to Use

  • Querying conversations, messages, or agents in LibreChat’s MongoDB
  • Any script that needs pymongo in the Railway environment

Connection

from pymongo import MongoClient

def get_env(key):
    with open("/proc/1/environ", "rb") as f:
        for entry in f.read().decode("utf-8", errors="replace").split("\0"):
            if entry.startswith(f"{key}="):
                return entry.split("=", 1)[1]
    raise RuntimeError(f"{key} not found in /proc/1/environ")

MONGO_URI = get_env("MONGO_URI")
db = MongoClient(MONGO_URI)["test"]

Python Packages in Railway

Packages are baked into the Docker image via Forge/Configs/LibreChat_Service/Dockerfile.librechat. To add a new package, add it to the pip install line in the Dockerfile and redeploy.

Installed packages:

  • pymongo — MongoDB driver
  • dnspython — pymongo dependency

Collections & Schemas

conversations

FieldTypeDescription
conversationIdstring (UUID)Primary identifier
titlestringConversation title
messageslist[ObjectId]References to messages collection
updatedAtdatetimeLast update timestamp
userObjectIdUser who owns the conversation

messages

FieldTypeDescription
messageIdstring (UUID)Primary identifier
conversationIdstring (UUID)Parent conversation
senderstring"User" or agent display name
isCreatedByUserboolTrue for user messages
textstringPlain text of the message (may be empty for tool-heavy turns)
contentlist[dict]Structured content items (see below)
tokenCountintToken count for the message
createdAtdatetimeCreation timestamp

content item types

text: {"type": "text", "text": "..."}

tool_call:

{
  "type": "tool_call",
  "tool_call": {
    "id": "call_...",
    "name": "tool_name",
    "args": "{\"param\": \"value\"}",
    "output": "result text",
    "progress": 1
  }
}

agents

FieldTypeDescription
idstringAgent ID (agent_...)
namestringDisplay name
instructionsstringSystem prompt / instructions
model_parametersdictModel-specific settings
toolslistEnabled tools

Rules

  • Never expose MONGO_URI in conversation output — redact in transcripts
  • MONGO_URI is read from /proc/1/environ — never hardcode connection strings
  • Always close connections or use context managers for scripts that run standalone