Skip to main content

Setup

Follow the instructions in the MongoDB Setup Guide to get connection string Install MongoDB packages
pip install "pymongo[srv]"

Example

agent_with_knowledge.py
from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.mongodb import MongoVectorDb

# 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 = Knowledge(
    vector_db=MongoVectorDb(
        collection_name="recipes",
        db_url=mdb_connection_string,
        wait_until_index_ready_in_seconds=60,
        wait_after_insert_in_seconds=300
    ),
)  # adjust wait_after_insert_in_seconds and wait_until_index_ready_in_seconds to your needs

if __name__ == "__main__":
    knowledge_base.add_content(
        url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
    )

    agent = Agent(knowledge=knowledge_base)
    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.knowledge import Knowledge
from agno.vectordb.mongodb import MongoVectorDb

# 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 = Knowledge(
    vector_db=MongoVectorDb(
        collection_name="recipes",
        db_url=mdb_connection_string,
    ),
)

# Create and use the agent
agent = Agent(knowledge=knowledge_base)

if __name__ == "__main__":
    # Load knowledge base asynchronously
    asyncio.run(knowledge_base.add_content_async(
            url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
        )
    )

    # Create and use the agent asynchronously
    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

ParameterTypeDescriptionDefault
collection_namestrName of the MongoDB collectionRequired
nameOptional[str]Name of the vector databaseNone
descriptionOptional[str]Description of the vector databaseNone
idOptional[str]Unique identifier for the vector databaseAuto-generated
db_urlOptional[str]MongoDB connection string"mongodb://localhost:27017/"
databasestrDatabase name"agno"
embedderOptional[Embedder]Embedder instance for generating embeddingsOpenAIEmbedder()
distance_metricstrDistance metric for similarityDistance.cosine
overwriteboolOverwrite existing collection and index if TrueFalse
wait_until_index_ready_in_secondsOptional[float]Time in seconds to wait until the index is ready3
wait_after_insert_in_secondsOptional[float]Time in seconds to wait after inserting documents3
max_pool_sizeintMaximum number of connections in the connection pool100
retry_writesboolWhether to retry write operationsTrue
clientOptional[MongoClient]An existing MongoClient instanceNone
search_index_nameOptional[str]Name of the search index"vector_index_1"
cosmos_compatibilityOptional[bool]Whether to use Azure Cosmos DB MongoDB vCore compatibility modeFalse
search_typeSearchTypeThe search type to use when searching for documentsSearchType.vector
hybrid_vector_weightfloatDefault weight for vector search results in hybrid search0.5
hybrid_keyword_weightfloatDefault weight for keyword search results in hybrid search0.5
hybrid_rank_constantintDefault rank constant (k) for Reciprocal Rank Fusion in hybrid search60

Developer Resources