MongoDB Knowledge
Use MongoDB Atlas as a scoped, vector-searchable knowledge backend
Store agent knowledge in MongoDB Atlas with per-tenant user_id / agent_id / run_id scope on every read and write.
Quick Start
- Agent with MongoDB knowledge
python from praisonaiagents import Agent, MemoryConfig agent = Agent( name="SupportBot", instructions="Answer using the customer's session history.", knowledge={ "vector_store": { "provider": "mongodb", "config": { "connection_string": "mongodb+srv://...", "database": "praison", "collection": "support_knowledge", }, }, }, memory=MemoryConfig(user_id="customer_42", session_id="session_2026_08_09"), ) agent.start("What did we agree on for the refund?") - Direct Knowledge API
python from praisonaiagents import Knowledge kb = Knowledge(config={ "vector_store": { "provider": "mongodb", "config": { "connection_string": "mongodb+srv://...", "database": "praison", "collection": "support_knowledge", }, } }) kb.add("Refund promised: $49 by Aug 12.", user_id="customer_42", agent_id="support_bot_v1", run_id="session_2026_08_09") kb.search("refund promise", user_id="customer_42", agent_id="support_bot_v1", run_id="session_2026_08_09")
How It Works
The adapter persists user_id / agent_id / run_id on each document and applies any provided scope as a pre-filter on search.
On Atlas $vectorSearch, the scope becomes the stage-level filter. Without Atlas vector search, the adapter falls back to MongoDB text search and merges the scope into the find() query. None values are dropped — omit an identifier to broaden the search on that dimension.
Full Method Surface
Since PR #5320 the MongoDB adapter implements the full KnowledgeStoreProtocol. Methods taking scope require at least one of user_id / agent_id / run_id; get / update / delete operate on a specific item_id and need no scope.
| Method | Signature (key args) | Returns | Raises | |
|---|---|---|---|---|
add(text, metadata?, user_id?, agent_id?, run_id?) | ≥1 scope required | str (document ID) | ScopeRequiredError | |
search(query, limit=5, user_id?, agent_id?, run_id?, filters?) | ≥1 scope required | List[dict] | ScopeRequiredError | |
get_all(user_id?, agent_id?, run_id?, limit=100) | ≥1 scope required | SearchResult | ScopeRequiredError | |
delete_all(user_id?, agent_id?, run_id?) | ≥1 scope required | bool | ScopeRequiredError | |
get(item_id) | — | `SearchResultItem \ | None` | — |
update(item_id, content, metadata?) | — | AddResult | — | |
delete(document_id) | — | bool | — |
Configuration Options
Config keys read from MongoDBKnowledgeAdapter.__init__ under vector_store.config.
| Option | Type | Default | Description |
|---|---|---|---|
connection_string | str | "mongodb://localhost:27017/" | MongoDB connection URI (use mongodb+srv://... for Atlas) |
database | str | "praisonai" | Database name |
collection | str | "knowledge_base" | Collection name |
use_vector_search | bool | True | Enable Atlas $vectorSearch; falls back to text search when unavailable |
Embeddings are configured via the top-level embedder key. The adapter recombines it into a litellm string as "<provider>/<model>" — except openai, which stays bare. It falls back to text-embedding-3-small when nothing is set:
config = {
"vector_store": {
"provider": "mongodb",
"config": {
"connection_string": "mongodb+srv://...",
"database": "praison",
"collection": "support_knowledge",
},
},
"embedder": {
"provider": "openai",
"config": {"model": "text-embedding-3-small"},
},
}Run knowledge fully local by pointing the same embedder block at Ollama:
config = {
"vector_store": {
"provider": "mongodb",
"config": {
"connection_string": "mongodb://localhost:27017/",
"database": "praison",
"collection": "support_knowledge",
},
},
"embedder": {
"provider": "ollama",
"config": {"model": "nomic-embed-text"},
},
}Common Patterns
Isolate a single customer’s session by combining all three scopes:
kb.search("refund promise",
user_id="customer_42", agent_id="support_bot_v1", run_id="session_2026_08_09")Broaden across all sessions for one customer by omitting run_id:
kb.search("refund history", user_id="customer_42")Narrow the search to a single metadata field with filters=:
# Narrow the search to a single metadata field
kb.search("policy questions",
user_id="customer_42",
filters={"category": "policy"})The MongoDB adapter silently dropped filters before PR #5320; it now folds each key in as metadata.<key> alongside the scope on both Atlas $vectorSearch and the text-search fallback.
Best Practices
Use Atlas for vector search
use_vector_search=True needs a MongoDB Atlas cluster with a vector index on the embedding path (similarity cosine). Without Atlas, the adapter transparently falls back to text search.
Always pass scope for multi-tenant apps
Provide user_id (and optionally agent_id / run_id) on both add() and search() to keep tenants isolated. Omitted scopes broaden the search on that dimension.
Prefer the Agent API
Passing knowledge={...} and user_id=... on the Agent handles scoping and context injection automatically. Use the direct Knowledge class only for custom indexing or search control.
Use mongodb+srv:// for Atlas connections
Atlas connection strings start with mongodb+srv:// or contain mongodb.net — the adapter detects these to enable vector search.