npm package: @jorgelg/mongodb
Description: --- title: 'MongoDB Database Integration' excerpt: 'The MongoDB Jovo integration allows you to store user specific data and more in a MongoDB database.' ---
Author: jovotech
Latest version: 4.2.36

---
title: 'MongoDB Database Integration'
excerpt: 'The MongoDB Jovo integration allows you to store user specific data and more in a MongoDB database.'
---

# MongoDB Database Integration

This [database integration](https://www.jovo.tech/docs/databases) allows you to store user specific data in a MongoDB table.

## Introduction

[MongoDB](https://www.mongodb.com/) is ... Shared Atlas clusters. Easy config.

## Installation

You can install the plugin like this:

```sh
$ npm install @jovotech/db-mongodb
```

Add it as plugin to any stage you like, e.g. `app.prod.ts`:

```typescript
import { MongoDb } from '@jovotech/db-mongodb';

// ...

app.configure({
  plugins: [
    MongoDb.instance({
      // Configuration
    }),
    // ...
  ],
});
```

Once the configuration is done, the MongoDB database integration will create a MongoDB database and a collection on the first read/write attempt (might take some seconds). No need for you to create the database.

The rest of this section provides an introduction to the steps you need to take depending on where you host your Jovo app:

- [On MongoDB Atlas](#for-apps-hosted-on-mongodb-atlas)

The [configuration section](#configuration) then provides a detailed overview of all configuration options.


## Configuration

The following configurations can be added:

```typescript
MongoDb.instance({
  connectionString: string;
  databaseName?: string;
  collectionName?: string;
} as MongoDbConfig),

```
- MongoDbConfig:
- ``connectionString``: Specify username, password and clusterUrl. Additional parameters can also be added. Have a look at the [MongoDB documentation](https://docs.mongodb.com/drivers/node/current/fundamentals/connection/#connection-uri) for more details.
- ``databaseName``: The name of the database we want to use. If not provided, use database name from connection string. A new database is created if doesn't exist yet.
- ``collectionName``: A new collection is created with that name if doesn't exist yet.

You should specify a dataBase either in ``connectionString`` or in ``databaseName``. The second takes precedence.
The default collection name is `users_all`.

Note that you can add timeout or other configuration by adding parameters in the ``connectionString``.

### Examples of use

For better performance, you can reuse the connection in any component like in this example:

```typescript
async START() {
    // You can read data from another user
    const users = mongoDb.jovoUsers();
    const otherUserData = (await users).find({ _id: '<another_id>' });

    // Also store documents in other collections in the same DB Jovo handles users
    const defaultDb = await mongoDb.jovoDb();
    await defaultDb.collection('my-collection').insertOne({ foo: 'bar' });

    // Or just get the single client to open a transaction
    const client = await mongoDb.client;
    const transactionResults = await client.startSession().withTransaction(async () => {
      //  Modify some data
      // ....
    });

    // Or create a new DB
    const newDb = client.db("my_new_db");
}
```

For the next example, although Jovo will persist it asynchronously during the lifecycle, it will use the same connection pool from the examples above. Reusing the client will get better response times and lower costs.
```typescript
this.$user.data.foo = 'bar';
```