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

# MongoStorage

> MongoDB storage for document-based scalable systems

## Overview

`MongoStorage` provides a MongoDB-based storage backend. Ideal for document-based applications, flexible schemas, and horizontally scalable deployments.

## Install

<Note>
  Install the MongoDB storage optional dependency group:

  ```bash theme={null}
  uv pip install "upsonic[mongo-storage]"
  ```
</Note>

## Basic Usage

```python theme={null}
from upsonic import Agent, Task
from upsonic.storage.memory import Memory
from upsonic.storage.mongo import MongoStorage

storage = MongoStorage(
    db_url="mongodb://localhost:27017",
    db_name="agent_memory"
)

memory = Memory(
    storage=storage,
    session_id="session_001",
    user_id="user_123",
    full_session_memory=True,
    summary_memory=True,
    user_analysis_memory=True,
    model="anthropic/claude-sonnet-4-5"
)

agent = Agent("anthropic/claude-sonnet-4-5", memory=memory)

result1 = agent.do(Task("My name is Alice"))
result2 = agent.do(Task("What's my name?"))
print(result2)  # "Your name is Alice"
```

## Parameters

| Parameter | Type | Default | Description |
| - | - | - | - |
| `db_url` | `str \| None` | `None` | MongoDB connection URL |
| `db_client` | `MongoClient \| None` | `None` | Pre-existing pymongo client |
| `db_name` | `str \| None` | `"upsonic"` | Database name |
| `session_collection` | `str \| None` | `"upsonic_sessions"` | Sessions collection name |
| `user_memory_collection` | `str \| None` | `"upsonic_user_memories"` | User memory collection name |
| `knowledge_collection` | `str \| None` | `"upsonic_knowledge"` | Knowledge registry collection name (used by KnowledgeBase) |
| `id` | `str \| None` | auto-generated | Storage instance ID |

## Storage Type

`MongoStorage` is a **synchronous** storage implementation using pymongo driver.


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