# @jorgelg/mongodb

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

Latest version **4.2.36** (published 2022-02-24) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @jorgelg/mongodb
pnpm add @jorgelg/mongodb
yarn add @jorgelg/mongodb
bun add @jorgelg/mongodb
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.2.36 |
| Published | 2022-02-24 |
| First published | 2022-02-02 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 28.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | jovotech |
| Maintainers | jorgelg |

## Links

- npm: https://www.npmjs.com/package/@jorgelg/mongodb
- npm.io page: https://npm.io/package/@jorgelg/mongodb

## Dependencies (1)

- [mongodb](https://npm.io/package/mongodb.md) ^4.3.1

## Recent versions

- 4.2.36 (latest) — 2022-02-24
- 4.2.35 — 2022-02-24
- 4.2.34 — 2022-02-17
- 4.2.33 — 2022-02-04
- 4.2.32 — 2022-02-04
- 4.2.30 — 2022-02-04
- 4.2.29 — 2022-02-04
- 4.2.28 — 2022-02-03
- 4.2.27 — 2022-02-03
- 4.2.26 — 2022-02-03
- 4.2.25 — 2022-02-03
- 4.2.24 — 2022-02-03
- 4.2.23 — 2022-02-03
- 4.2.22 — 2022-02-03
- 4.2.21 — 2022-02-03
- … 21 more at https://npm.io/package/@jorgelg/mongodb/versions

## README

---
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';
```

---
_Source: https://npm.io/package/@jorgelg/mongodb · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
