> ## 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.

# Memory with MongoDB

> Store user memories in MongoDB by passing MongoDb as the agent's database.

## Code

```python mem-mongodb-memory.py theme={null}
from agno.agent import Agent
from agno.db.mongo import MongoDb

# Setup MongoDb
db_url = "mongodb://mongoadmin:secret@localhost:27017"

db = MongoDb(db_url=db_url)

agent = Agent(
    db=db,
    update_memory_on_run=True,
)

agent.print_response("My name is John Doe and I like to play basketball on the weekends.")
agent.print_response("What do I do on weekends?")
```

## Usage

<Snippet file="create-venv-step.mdx" />

**1. Set environment variables**

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

**2. Install dependencies**

```bash theme={null}
uv pip install -U agno openai pymongo
```

**3. Run MongoDB**

Install [Docker Desktop](https://docs.docker.com/get-started/get-docker/), then start MongoDB on port `27017`:

```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
```

**4. Run Example**

```bash Mac/Linux theme={null}
python mem-mongodb-memory.py
```

```bash Windows theme={null}
python mem-mongodb-memory.py
```


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