# ISSUE: Plug multiple NoSQL databases

**URL:** <https://community.forestadmin.com/t/issue-plug-multiple-nosql-databases/3248>\
**Category:** Help me!\
**Created:** [September 8, 2021, 2:53pm UTC](https://community.forestadmin.com/t/issue-plug-multiple-nosql-databases/3248 "2021-09-08T14:53:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![usr1vang](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/usr1vang/32/3017_2.png) [@usr1vang](https://community.forestadmin.com/u/usr1vang)\
**Post date:** [September 8, 2021, 2:53pm UTC](https://community.forestadmin.com/t/issue-plug-multiple-nosql-databases/3248/1 "2021-09-08T14:53:13Z")

</div>

Hey 😀 I’ve integrate forest admin to my proyect with MongoDB which creates a new backend application connected to my NoSQL database.

All that works perfectly. The problem is when I want to connect a second NoSQL database. I followed the steps here: [https://docs.forestadmin.com/documentation/how-tos/databases/plug-multiple-nosql-databases](https://docs.forestadmin.com/documentation/how-tos/databases/plug-multiple-nosql-databases) but there is no way it works. It seems to me that the document is not updated because it does not say anything about what to do with the file config/database.js (attached below) and in step 6 it says to declare the connections in middlewares/forestadmin/index.js folder and file that does not exist.

I would appreciate if you could help me to connect the second database to forest as any more specifc guide.

Currently I only have one connected as it is configured by following the steps to connect your project to forest with MongoDB by running the commands:

- forest projects:create “hjb” --databaseConnectionURL “mongodb://localhost:27017/db1” --applicationHost “localhost” --applicationPort “3310”
- cd “hjb”
- npm install -s

file config/database.js:

 ![Screen Shot 2021-09-08 at 11.42.34](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/3/3711d8f6453a1a8fc29ded90d38c31e715152b3e.png)

folders created by forest config:

 ![Screen Shot 2021-09-08 at 11.46.10](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/e/e6e3b4e3dbd3862b632fda4811ca65e882dde8f3.png)

Thanks!

Set up information:  
“express”: “~4.17.1”,  
“forest-express-mongoose”: “^8.0.0”,  
“mongoose”: “~5.8.2”,

---

<div class="post-metadata">

**Author:** ![Sliman\_Medini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sliman_medini/32/19_2.png) [@Sliman\_Medini](https://community.forestadmin.com/u/Sliman_Medini)\
**Post date:** [September 8, 2021, 3:36pm UTC](https://community.forestadmin.com/t/issue-plug-multiple-nosql-databases/3248/2 "2021-09-08T15:36:38Z")

</div>

Hello @usr1vang,

It seems we miss a doc update.  
Here is what you have to do to add second mongo database to your project:

- (We assume you have a project working with the first database)
- remove the models files under `/models` **except** `index.js` (they will be regenerated later)
- Inside `.env` file add another connection string definition (for example `DATABASE_URL_2=...`) pointing to you second database
- Inside `config/databases.js`
  - update `modelsDir` value: ‘…/models/ **db1** ’ (append `/db1`)
  - you can update the `name` if you wish
  - add a new entry to the exported array (you can clone the first one)
    - replace `db1` by `db2`
    - change the `name`

- run forest `schema:update` from the project root

Thank for pointing the issue, we will fix it 🙏

---

<div class="post-metadata">

**Author:** ![usr1vang](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/usr1vang/32/3017_2.png) [@usr1vang](https://community.forestadmin.com/u/usr1vang)\
**Post date:** [September 8, 2021, 8:21pm UTC](https://community.forestadmin.com/t/issue-plug-multiple-nosql-databases/3248/3 "2021-09-08T20:21:38Z")

</div>

Hi @Sliman_Medini thanks you for your quickly response. I have followed your steps and everything seems to be working until I go to [https://app.forestadmin.com/projects](https://app.forestadmin.com/projects).

The problem now is that in Forest only shows the collections of the second database (db2) and it is not that they are not visible, they do not even appear.

What could be wrong here? maybe I am missing something inside middlewares/forestadmin.js related to the database connections? below i attached how my project folders looks like and the middlewares/forestadmin.js file.

 ![Screen Shot 2021-09-08 at 17.19.13](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/1/15ce545c88a3b93722a8a72fb605f072d904aeed.png)

I look forward to your comments.  
Thanks!

---

<div class="post-metadata">

**Author:** ![Sliman\_Medini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sliman_medini/32/19_2.png) [@Sliman\_Medini](https://community.forestadmin.com/u/Sliman_Medini)\
**Post date:** [September 9, 2021, 8:03am UTC](https://community.forestadmin.com/t/issue-plug-multiple-nosql-databases/3248/4 "2021-09-09T08:03:48Z")

</div>

Ok,

You can double-check the collections from database A are missing by looking at the collections list defined in `forestadmin-schema.json`.  
Assuming this is the case, you may have one of this problem:

- the names of the databases are the same in `config/database.js` ?
- the content of the `models/index.js` has changed ? (the old documentation asks to alter this file)

Do not hesitate to share to `config/database.js` if you need.

The content of `index.js` should be:

```javascript
const fs = require('fs');
const path = require('path');
const Mongoose = require('mongoose');

const databasesConfiguration = require('../config/databases');

const connections = {};
const db = {};

databasesConfiguration.forEach((databaseInfo) => {
  const connection = Mongoose.createConnection(databaseInfo.connection.url, databaseInfo.connection.options);
  connections[databaseInfo.name] = connection;

  const modelsDir = databaseInfo.modelsDir || path.join(__dirname, databaseInfo.name);
  fs
    .readdirSync(modelsDir)
    .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js')
    .forEach((file) => {
      try {
        const model = require(path.join(modelsDir, file))(connection, Mongoose);
        db[model.modelName] = model;
      } catch (error) {
        console.error(`Model creation error: ${error}`);
      }
    });
});

db.objectMapping = Mongoose;
db.connections = connections;

module.exports = db;

```

Regards

---

<div class="post-metadata">

**Author:** ![usr1vang](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/usr1vang/32/3017_2.png) [@usr1vang](https://community.forestadmin.com/u/usr1vang)\
**Post date:** [September 9, 2021, 10:07am UTC](https://community.forestadmin.com/t/issue-plug-multiple-nosql-databases/3248/5 "2021-09-09T10:07:16Z")

</div>

Hi,

Indeed i had a problem with the name field in the config/databases.js file. I modified that and it is working, thank you very much for your help.

Regards.
