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.
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:
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",
},
},
)How It Works
Quick Start
- Simple Usage
python from praisonaiagents import Agent agent = Agent( name="assistant", memory={ "provider": "mongodb", "config": { "connection_string": "mongodb://localhost:27017/", "database": "praisonai", }, }, ) - 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
| Option | Type | Default | Description |
|---|---|---|---|
connection_string | str | mongodb://localhost:27017/ | MongoDB URI |
database | str | praisonai | Database name |
use_vector_search | bool | False | Enable Atlas Vector Search on short-term and long-term memory |
embedding_model | str | "text-embedding-3-small" | Embedding model as a litellm string (e.g. "ollama/nomic-embed-text"). Wins over embedder. |
embedder | dict | None | Alternative shape: {"provider": "...", "config": {"model": "..."}}. Recombined into "<provider>/<model>" for litellm; openai stays bare. |
max_pool_size | int | 50 | Connection pool maximum |
min_pool_size | int | 10 | Connection pool minimum |
max_idle_time | int | 30000 | Max idle time in ms |
server_selection_timeout | int | 5000 | Server 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-smallby default, or whatever you configure (see below). - Searches use MongoDB
$vectorSearchagainst indexvector_indexon fieldembedding— 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):
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):
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
- Create a vector search index named
vector_indexon both theshort_term_memoryandlong_term_memorycollections. - Set the indexed path to
embeddingon each. - Pass
use_vector_search: Truein 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
Registry pattern and custom backend registration.
Memory Advanced SearchReranking, relevance cutoffs, and quality filtering.
Dakera MemorySelf-hosted, decay-weighted vector recall via the Dakera server.
Local Memory & KnowledgeRun memory and knowledge fully local with Ollama embeddings.
Ollama EmbeddingsLocal embedding models and their auto-detected dimensions.