---
hide_table_of_contents: true
---

import CodeBlock from "@theme/CodeBlock";

# MongoDB Chat Memory

For longer-term persistence across chat sessions, you can swap out the default in-memory `chatHistory` that backs chat memory classes like `BufferMemory` for a MongoDB instance.

## Setup

You need to install Node MongoDB SDK in your project:

```bash npm2yarn
npm install -S mongodb
```

You will also need a MongoDB instance to connect to.

## Usage

Each chat history session stored in MongoDB must have a unique session id.

import Example from "@examples/memory/mongodb.ts";

<CodeBlock language="typescript">{Example}</CodeBlock>
