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

# Use AsyncMongoDb as the database for an agent.

> Async MongoDB storage for agent sessions and history.

Install dependencies:

```bash theme={null}
uv pip install openai pymongo motor
```

Run a local MongoDB server using:

```bash theme={null}
docker run -d \
  --name local-mongo \
  -p 27017:27017 \
  -e MONGO_INITDB_ROOT_USERNAME=mongoadmin \
  -e MONGO_INITDB_ROOT_PASSWORD=secret \
  mongo
```

or use our script:

```bash theme={null}
./scripts/run_mongodb.sh
```

```python theme={null}
import asyncio

from agno.agent import Agent
from agno.db.mongo import AsyncMongoDb
from agno.tools.websearch import WebSearchTools

# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
db_url = "mongodb://mongoadmin:secret@localhost:27017"
db = AsyncMongoDb(db_url=db_url)

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
    db=db,
    tools=[WebSearchTools()],
    add_history_to_context=True,
)

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    asyncio.run(agent.aprint_response("How many people live in Canada?"))
    asyncio.run(agent.aprint_response("What is their national anthem called?"))
```

## Run the Example

```bash theme={null}
# Clone and setup repo
git clone https://github.com/agno-agi/agno.git
cd agno/cookbook/06_storage/mongo/async_mongo

# Create and activate virtual environment
./scripts/demo_setup.sh
source .venvs/demo/bin/activate

python async_mongodb_for_agent.py
```
