Setup
Follow the instructions in the MongoDB Setup Guide to get connection string Install MongoDB packagespip install "pymongo[srv]"
Example
agent_with_knowledge.py
from agno.agent import Agent
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.vectordb.mongodb import MongoDb
# MongoDB Atlas connection string
"""
Example connection strings:
"mongodb+srv://<username>:<password>@cluster0.mongodb.net/?retryWrites=true&w=majority"
"mongodb://localhost/?directConnection=true"
"""
mdb_connection_string = ""
knowledge_base = PDFUrlKnowledgeBase(
urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=MongoDb(
collection_name="recipes",
db_url=mdb_connection_string,
wait_until_index_ready=60,
wait_after_insert=300
),
) # adjust wait_after_insert and wait_until_index_ready to your needs
# knowledge_base.load(recreate=True) # Comment out after first run
agent = Agent(knowledge=knowledge_base, show_tool_calls=True)
agent.print_response("How to make Thai curry?", markdown=True)
Async Support ⚡
MongoDB also supports asynchronous operations, enabling concurrency and leading to better performance.
async_mongodb.py
import asyncio
from agno.agent import Agent
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.vectordb.mongodb import MongoDb
# MongoDB Atlas connection string
"""
Example connection strings:
"mongodb+srv://<username>:<password>@cluster0.mongodb.net/?retryWrites=true&w=majority"
"mongodb://localhost:27017/agno?authSource=admin"
"""
mdb_connection_string = "mongodb+srv://<username>:<password>@cluster0.mongodb.net/?retryWrites=true&w=majority"
knowledge_base = PDFUrlKnowledgeBase(
urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=MongoDb(
collection_name="recipes",
db_url=mdb_connection_string,
),
)
# Create and use the agent
agent = Agent(knowledge=knowledge_base, show_tool_calls=True)
if __name__ == "__main__":
# Comment out after the first run
asyncio.run(knowledge_base.aload(recreate=False))
asyncio.run(agent.aprint_response("How to make Thai curry?", markdown=True))
Use
aload() and aprint_response() methods with asyncio.run() for non-blocking operations in high-throughput applications.MongoDB Params
| Parameter | Type | Description | Default |
|---|---|---|---|
collection_name | str | Name of the MongoDB collection | Required |
db_url | Optional[str] | MongoDB connection string | "mongodb://localhost:27017/" |
database | str | Database name | "agno" |
embedder | Optional[Embedder] | Embedder instance for generating embeddings | OpenAIEmbedder() |
distance_metric | str | Distance metric for similarity | Distance.cosine |
overwrite | bool | Overwrite existing collection and index if True | False |
wait_until_index_ready | Optional[float] | Time in seconds to wait until the index is ready | None |
wait_after_insert | Optional[float] | Time in seconds to wait after inserting documents | None |
max_pool_size | int | Maximum number of connections in the connection pool | 100 |
retry_writes | bool | Whether to retry write operations | True |
client | Optional[MongoClient] | An existing MongoClient instance | None |
Developer Resources
- View Cookbook (Sync)
- View Cookbook (Async)