# MongoDB

Source: https://docs.interop.io/manager/databases/mongo/index.html

## Overview

**io.Manager** supports connecting to MongoDB databases. The following sections outline the steps and requirements for configuring **io.Manager** to work with MongoDB databases.

> ℹ️ *For a complete example of connecting **io.Manager** to a MongoDB database, see the [MongoDB Database](https://github.com/InteropIO/manager-examples/tree/main/db-mongo) example on GitHub.*

## Connecting to MongoDB Databases

To configure **io.Manager** to connect to a MongoDB database, you must either set the necessary environment variables, or provide the required settings via the configuration object for initializing the **io.Manager** Server.

Depending on the [deployment](https://docs.interop.io/manager/deployment/index.md) approach you have chosen, you have the following options:

- If you are using the [basic deployment scenario](https://github.com/InteropIO/manager-examples/tree/main/manager-template/1-basic) from the [template repository](https://docs.interop.io/manager/deployment/index.md#template_repository) approach, you must set properly the necessary environment variables.

- If you are using the [NPM packages](https://docs.interop.io/manager/deployment/index.md#npm_packages) for deployment, or the [advanced deployment scenario](https://github.com/InteropIO/manager-examples/tree/main/manager-template/2-advanced) from the template repository approach, you must provide the necessary settings via the configuration object for initializing the **io.Manager** Server.

The following sections provide examples of both options.

### Environment Variables

To configure **io.Manager** to connect to a MongoDB database, you must register all of the following environment variables with the proper values. The `API_STORE_TYPE` environment variable must be set to `mongo`. All other variables must be set with values according to your specific environment:

| Environment Variable | Description |
|----------------------|-------------|
| `API_STORE_TYPE` | **Required.** Type of the database. Must be set to `mongo`. |
| `API_STORE_MONGO` | **Required.** MongoDB connection URL. |
| `API_STORE_MONGO_TRANSACTIONS` | Whether to use MongoDB multi-document transactions, which require MongoDB to be deployed as a replica set or a sharded cluster. Set to `required` to prevent the server from starting when the MongoDB deployment doesn't support transactions, or to `disabled` to never use transactions. Defaults to `autodetect` - transactions are used when the deployment supports them, otherwise the server logs a warning and runs without transactions. *Available since **io.Manager** 4.0.* |

Example settings:

```cmd
API_STORE_TYPE=mongo
API_STORE_MONGO=mongodb://localhost:27017/my_db
```

> ℹ️ *For details on all available environment variables for configuring the **io.Manager** Server, see the [Configuration > Server](https://docs.interop.io/manager/configuration/server/index.md#environment_variables) section.*

### Configuration Object

To configure **io.Manager** to connect to a MongoDB database, you must provide the necessary settings when initializing the **io.Manager** Server. Use the `store` property of the optional `Config` object and provide a `MongoStoreConfig` object as its value.

The following example demonstrates configuring the connection to a MongoDB database when initializing the **io.Manager** Server:

```javascript
import { start } from "@interopio/manager";

// Configuration for the io.Manager Server.
const config = {
    name: "my-server",
    port: 4242,
    token: {
        secret: "my-secret"
    },
    // Configuration for connecting to a MongoDB database.
    store: {
        type: "mongo",
        connection: "mongodb://localhost:27017/my_db"
    }
};

// Initializing the io.Manager Server.
const server = await start(config);
```

The `store` object has the following properties:

| Property | Type | Description |
|----------|------|-------------|
| `connection` | `string` | **Required.** MongoDB connection URL. |
| `transactions` | `"disabled"` \| `"required"` \| `"autodetect"` | Whether to use MongoDB multi-document transactions, which require MongoDB to be deployed as a replica set or a sharded cluster. Set to `"required"` to prevent the server from starting when the MongoDB deployment doesn't support transactions, or to `"disabled"` to never use transactions. Defaults to `"autodetect"` - transactions are used when the deployment supports them, otherwise the server logs a warning and runs without transactions. *Available since **io.Manager** 4.0.* |
| `type` | `"mongo"` | **Required.** Type of the data store. Must be set to `"mongo"` when using a MongoDB database. |

> ℹ️ *For details on all available properties for configuring the **io.Manager** Server, see the [Configuration > Server](https://docs.interop.io/manager/configuration/server/index.md#configuration_object) section.*
