> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# MongoDB Vector Database

> Use MongoDB as a vector database for your Knowledge Base.

## Setup

Follow the instructions in the [MongoDB Setup Guide](https://www.mongodb.com/docs/atlas/getting-started/) to get your connection string.

Install dependencies

```shell theme={null}
uv pip install -U agno "pymongo[srv]" pypdf openai
```

The example uses OpenAI for embeddings and the agent model, so set your API key:

```shell theme={null}
export OPENAI_API_KEY=xxx
```

## Example

```python agent_with_knowledge.py theme={null}
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 = "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,
        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.insert(
        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.
>
> ```python async_mongodb.py theme={null}
> 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.ainsert(
>             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))
> ```
>
> > [!TIP]
> >
> > Use `ainsert()` and `aprint_response()` methods with `asyncio.run()` for non-blocking operations in high-throughput applications.

## MongoDB Params

<Snippet file="vectordb_mongodb_params.mdx" />


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.