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 persistsuser_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 fullKnowledgeStoreProtocol. 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 fromMongoDBKnowledgeAdapter.__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:
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:run_id:
filters=:
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 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
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
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
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.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.Related
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

