Skip to main content
Store agent knowledge in MongoDB Atlas with per-tenant user_id / agent_id / run_id scope on every read and write.

Quick Start

1

Agent with MongoDB knowledge

2

Direct Knowledge API


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.

Configuration Options

Config keys read from MongoDBKnowledgeAdapter.__init__ under vector_store.config. 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:
Run knowledge fully local by pointing the same embedder block at Ollama:
Atlas vector search indexes are built for a specific dimension. Switching the embedder model β€” e.g. from text-embedding-3-small (1536) to nomic-embed-text (768) β€” means you must re-create the vector index on the collection at the new dimension, or searches will error or return nothing.

Common Patterns

Isolate a single customer’s session by combining all three scopes:
Broaden across all sessions for one customer by omitting run_id:
Narrow the search to a single metadata field with filters=:
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_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.
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.
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.
Atlas connection strings start with mongodb+srv:// or contain mongodb.net β€” the adapter detects these to enable vector search.
Since PR #3810, the MongoDB adapter honors user_id / agent_id / run_id on add() and search(). Since PR #5320, it also enforces those scopes: an unscoped add() / search() / get_all() / delete_all() raises ScopeRequiredError (previously an unscoped search() silently returned every tenant’s documents). The adapter also now defines get_all(), delete_all(), update(), and get() β€” earlier releases raised AttributeError for those calls on a MongoDB-backed Knowledge.

Knowledge Backends

Compare Chroma, mem0, and MongoDB storage backends

MongoDB Memory

Use MongoDB as the agent memory store

Local Memory & Knowledge

Run memory and knowledge fully local with Ollama embeddings

Ollama Embeddings

Local embedding models and their auto-detected dimensions