# MongoDB (https://www.librechat.ai/docs/user_guides/mongodb)

MongoDB, a popular NoSQL database, was chosen as the core database for LibreChat due to its flexibility, scalability, and ability to handle diverse data structures efficiently. Here are some key reasons why MongoDB is an excellent fit for LibreChat:

![MongoDB Compass viewing the LibreChat database](https://github.com/danny-avila/LibreChat/assets/32828263/84d4a608-1f73-41c9-a026-7772a74d205b)

## 1. Flexible Data Model
MongoDB's document-based data model allows for storing and retrieving data in a flexible and dynamic manner. Unlike traditional relational databases, MongoDB doesn't require a fixed schema, making it easier to adapt to changing data requirements. This flexibility is essential for LibreChat, as it needs to store various types of data, such as conversation histories, user profiles, presets, API keys, and more, without being constrained by a rigid table structure.

## 2. Efficient Storage of Conversation Histories
One of the primary use cases for LibreChat is to store and retrieve conversation histories. MongoDB's ability to store nested data structures as JSON-like documents makes it an excellent choice for storing conversation histories, which can include complex data structures like messages, timestamps, and metadata.

## 3. Secure Storage of Sensitive Data
LibreChat handles sensitive data, such as API keys and encrypted user passwords. MongoDB's built-in support for data encryption at rest and in transit ensures that this sensitive information remains secure and protected from unauthorized access.

## 4. Horizontal Scalability
As LibreChat grows and attracts more users, its data storage requirements will increase. MongoDB's horizontal scalability allows for scaling out by adding more servers to a cluster, providing the ability to handle larger amounts of data and higher traffic loads without compromising performance.

## 5. Cross-Device Accessibility
LibreChat aims to provide a seamless experience across multiple devices, allowing users to access their data and conversation histories from different devices. MongoDB's replication and sharding capabilities ensure that data is consistently available and accessible, enabling users to pick up their conversations where they left off, regardless of the device they're using.

## 6. Developer Productivity
MongoDB's intuitive query language and rich ecosystem of tools and libraries contribute to faster development cycles and increased developer productivity. This aligns well with LibreChat's goal of being an open-source project, fostering collaboration and contributions from the developer community.

By leveraging MongoDB's strengths, LibreChat can efficiently manage and store diverse data structures, ensure data security and availability, and provide a seamless cross-device experience for its users. MongoDB's flexibility, scalability, and developer-friendly features make it an ideal choice for powering the core functionalities of LibreChat.

## Amazon DocumentDB Compatibility

LibreChat supports **Amazon DocumentDB 5.0 or later instance-based clusters** as a deployment target. Configure `MONGO_URI` with `retryWrites=false`, because DocumentDB does not support retryable writes:

```bash filename=".env"
MONGO_URI=mongodb://<user>:<password>@<cluster>:27017/librechat?tls=true&tlsCAFile=/path/to/global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false
```

Use the current AWS CA bundle and the network path required for your VPC. See AWS's [functional differences from MongoDB](https://docs.aws.amazon.com/documentdb/latest/developerguide/functional-differences.html) and [connection guidance](https://docs.aws.amazon.com/documentdb/latest/developerguide/connect_programmatically.html).

DocumentDB 4.0 can run LibreChat with reduced database enforcement: it does not support the partial unique indexes LibreChat uses for OAuth identities, generated code files, and external group IDs. LibreChat logs index-build failures at startup, but those constraints are not enforced by the database. DocumentDB 5.0+ instance-based clusters support the required partial-index forms.

<Callout type="warning" title="Elastic clusters are unsupported">
  Amazon DocumentDB elastic clusters do not support unique indexes, including LibreChat's unique
  user identity index, and are not a supported LibreChat database target. See the AWS
  [elastic-cluster limitations](https://docs.aws.amazon.com/documentdb/latest/developerguide/docdb-using-elastic-clusters.html).
</Callout>

Do not run `Model.syncIndexes()` against DocumentDB as an operational migration step; DocumentDB's `collMod` support is limited. LibreChat's regular startup index creation is the intended path.


## Note

<Callout type="warning" title="CPU compatibility">
**Note:** If you're running LibreChat on a processor that doesn't have SSE4.2, AVX support, or other required CPU features, you'll need to use an older but compatible version of MongoDB with the Docker installation. Specifically, you should use the `mongo:4.4.18` image, which is compatible with processors without these features.

To use this older MongoDB version with the LibreChat Docker installation, you'll need to utilize the `docker-compose.override.yml` file. This override file allows you to specify the MongoDB version you want to use, overriding the default version included in the main `docker-compose.yml` file.

For more information on using the `docker-compose.override.yml` file and configuring an older MongoDB version for your Docker installation, please refer to our [Docker Override Configuration Guide](/docs/configuration/docker_override).
</Callout>
