"""Use MongoDb as the database for an agent.
Run `uv pip install openai pymongo` to install dependencies
Run a local MongoDB server using:
```bash
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
./scripts/run_mongodb.sh
```
"""
from agno.agent import Agent
from agno.db.mongo import MongoDb
from agno.tools.websearch import WebSearchTools
# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
db_url = "mongodb://mongoadmin:secret@localhost:27017"
db = MongoDb(db_url=db_url)
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
db=db,
tools=[WebSearchTools()],
add_history_to_context=True,
)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
agent.print_response("How many people live in Canada?")
agent.print_response("What is their national anthem called?")
Run the Example
# Clone and setup repo
git clone --branch v2.7.4 --depth 1 https://github.com/agno-agi/agno.git
cd agno
# Create and activate virtual environment
./scripts/demo_setup.sh
source .venvs/demo/bin/activate
uv pip install pymongo
./cookbook/scripts/run_mongodb.sh
python cookbook/06_storage/mongo/mongodb_for_agent.py
docker rm -f local-mongo