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

# AsyncMongoStorage

> Async MongoDB storage for document-based scalable systems

## Overview

`AsyncMongoStorage` provides an asynchronous MongoDB-based storage backend. Supports both Motor (legacy) and PyMongo async (recommended) clients. 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}
import asyncio
from upsonic import Agent, Task
from upsonic.storage.memory import Memory
from upsonic.storage.mongo import AsyncMongoStorage

async def main():
    storage = AsyncMongoStorage(
        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 = await agent.do_async(Task("My name is Alice"))
    result2 = await agent.do_async(Task("What's my name?"))
    print(result2)  # "Your name is Alice"

asyncio.run(main())
```

## Parameters

| Parameter | Type | Default | Description |
| - | - | - | - |
| `db_url` | `str` | `None` | MongoDB connection URL |
| `db_name` | `str` | `"upsonic"` | Database name to use |
| `db_client` | `AsyncIOMotorClient` or `AsyncMongoClient` | `None` | Pre-configured async MongoDB client |
| `session_collection` | `str` | `None` | Custom name for the session collection |
| `user_memory_collection` | `str` | `None` | Custom name for the user memory collection |
| `knowledge_collection` | `str` | `None` | Custom name for the knowledge registry collection (used by KnowledgeBase) |
| `id` | `str` | `None` | Unique identifier for this storage instance |

<Note>
  When both Motor and PyMongo async are available, PyMongo async is preferred. Install using `uv pip install -U 'pymongo>=4.9'` (recommended) or `uv pip install -U motor` (legacy).
</Note>


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