MongoDB Memory

Configure MongoDB and Atlas Vector Search for agent memory

Use MongoDB as a document-backed memory store with optional Atlas Vector Search for semantic retrieval.

python
from praisonaiagents import Agent

agent = Agent(
    name="assistant",
    instructions="Remember facts across sessions.",
    memory={
        "provider": "mongodb",
        "config": {
            "connection_string": "mongodb://localhost:27017/",
            "database": "praisonai",
            "use_vector_search": True,
            "embedder": {
                "provider": "ollama",
                "config": {"model": "nomic-embed-text"},
            },
        },
    },
)
agent.start("Remember that my favourite colour is teal")

The user chats with the agent; short- and long-term memory persist in MongoDB, embedded fully local through Ollama.

Prefer the shortest, flat form? Use embedding_model directly:

python
from praisonaiagents import Agent

agent = Agent(
    name="assistant",
    memory={
        "provider": "mongodb",
        "config": {
            "connection_string": "mongodb://localhost:27017/",
            "database": "praisonai",
            "use_vector_search": True,
            "embedding_model": "ollama/nomic-embed-text",
        },
    },
)
Diagram

MongoDB Memory

Agent

Memory

Short-term

Long-term

Vector Search

How It Works

Diagram
MongoDB MemoryAgentUserMongoDB MemoryAgentUserRequestProcess requestResult Agent–>>User: Response

Quick Start

  1. Simple Usage
    python
    from praisonaiagents import Agent
    
    agent = Agent(
        name="assistant",
        memory={
            "provider": "mongodb",
            "config": {
                "connection_string": "mongodb://localhost:27017/",
                "database": "praisonai",
            },
        },
    )
  2. With Configuration
    python
    import os
    from praisonaiagents import Agent
    
    agent = Agent(
        name="assistant",
        memory={
            "provider": "mongodb",
            "config": {
                "connection_string": os.getenv("MONGODB_URI", "mongodb://localhost:27017/"),
                "database": "praisonai",
                "use_vector_search": True,
            },
        },
    )

Configuration

OptionTypeDefaultDescription
connection_stringstrmongodb://localhost:27017/MongoDB URI
databasestrpraisonaiDatabase name
use_vector_searchboolFalseEnable Atlas Vector Search on short-term and long-term memory
embedding_modelstr"text-embedding-3-small"Embedding model as a litellm string (e.g. "ollama/nomic-embed-text"). Wins over embedder.
embedderdictNoneAlternative shape: {"provider": "...", "config": {"model": "..."}}. Recombined into "<provider>/<model>" for litellm; openai stays bare.
max_pool_sizeint50Connection pool maximum
min_pool_sizeint10Connection pool minimum
max_idle_timeint30000Max idle time in ms
server_selection_timeoutint5000Server selection timeout in ms

Vector Search

When use_vector_search: True:

  • Both short-term and long-term writes include an embedding — using text-embedding-3-small by default, or whatever you configure (see below).
  • Searches use MongoDB $vectorSearch against index vector_index on field embedding — the same field in both collections.
  • If vector search fails or is unavailable, either tier falls back to MongoDB text search.

When use_vector_search: False (default), only MongoDB text indexes are used.

Configuring the embedding model

The adapter resolves the embedding model at construction, in this precedence order: embedding_model → config["embedder"] → falls back to text-embedding-3-small.

Use the embedder block (preferred — the same shape used across memory, knowledge, and the MongoDB knowledge adapter):

python
from praisonaiagents import Agent

agent = Agent(
    name="assistant",
    memory={
        "provider": "mongodb",
        "config": {
            "connection_string": "mongodb://localhost:27017/",
            "database": "praisonai",
            "use_vector_search": True,
            "embedder": {
                "provider": "ollama",
                "config": {"model": "mxbai-embed-large"},
            },
        },
    },
)

Or the flat embedding_model string (litellm-style, shortest):

python
from praisonaiagents import Agent

agent = Agent(
    name="assistant",
    memory={
        "provider": "mongodb",
        "config": {
            "connection_string": "mongodb://localhost:27017/",
            "database": "praisonai",
            "use_vector_search": True,
            "embedding_model": "ollama/nomic-embed-text",
        },
    },
)

Atlas Setup

  1. Create a vector search index named vector_index on both the short_term_memory and long_term_memory collections.
  2. Set the indexed path to embedding on each.
  3. Pass use_vector_search: True in agent config.

Best Practices

Use environment variables for credentials

Store connection strings in MONGODB_URI rather than hard-coding credentials in source files.

Enable vector search for semantic recall

Set use_vector_search: True on Atlas when you need similarity search; text indexes suffice for keyword lookup.

Size the connection pool for concurrency

Tune max_pool_size and min_pool_size when running many agents against the same cluster.

Create indexes before production traffic

Configure the vector_index Atlas index and text indexes before scaling agent workloads.

Related