How to connect two MongoDB databases on my running Forest project

Hi Forest,

I need to plug a new MongoDB database to my running Forest project. (I already have one MongoDB database plugged.)

First of all, I am not sure which version of the Forest doc to follow: V8 ? V5 ? V4?

image
image

and I am not sure on which version I am currently running my project.

Here are my packages :

"forest-express-mongoose": "7.8.2",
"forest-express-sequelize": "7.11.2",

Then, I have tried to configure the databases.js as follow:

module.exports = [
  {
    name: MONGO_DB,
    modelsDir: path.resolve(__dirname, '../models/joinly'),
    connection: {
      url: createMongoURI(), // => function that build the url with env vars.
      options: createMongoConnectOptions()
    }
  },
  {
    name: PING_EXT_MONGO_NAME,
    modelsDir: path.resolve(__dirname, '../models/ping-ext'),
    connection: {
      url: PING_EXT_MONGO_URL // => url directly stored in this env var.
    }
  }
]

I can display the new collections from the second MongoDB on Forest UI :
image

However, all the collections are using the first DB connection:

  • if a collection A from the second MongoDB has the same name than a collection B in the first MongoDB, the collection A displays records of the collection B:

Records displayed in Comptes utilisateurs (PE) come from first MongoDB (MONGO_DB)

  • if no collection from the first MongoDB has the same name than a collection A from the second MongoDB, the collection A cannot display any records.

Can you help me fix this ?

Best

Hello @guillaumejauffret

To better help you we need some information, can you please fill the followings :

Context

  • Project name: …
  • Team name: …
  • Environment name: …
  • Agent (forest package) name & version: …
  • Database type: …
  • Recent changes made on your end if any: …

yes sorry @nbouliol

  • Project name: joinly-forest
  • Team name: Admins
  • Environment name: Docker_development
  • Agent (forest package) name & version:
"forest-express-mongoose": "7.8.2",
"forest-express-sequelize": "7.11.2"
{
    "liana": "forest-express-mongoose",
    "liana_version": "7.8.2",
    "stack": {
      "database_type": "multiple",
      "engine": "nodejs",
      "engine_version": "18.12.1",
      "orm_version": "5.8.13"
    }
  }
  • Database type: MongoDB

Does that help you @nbouliol ?

@nbouliol can someone help me ?

Hello @guillaumejauffret,

Can you send me the directory structure of your “models” folder ?

Here it is:

image

image

I’ll take this thread over, let me try to replicate this locally and come back to you

ok thank you @anon39940173 let me now as soon as possible :wink: thanks

You are using forest-express-mongoose@7.8.2.
On the documentation portal you should click on Legacy > Express, and then choose V7 (v6 rails).

You should depend on either one of those packages, but not both.
If you are using MongoDB, you should use the former and remove the other one.

I can replicate this.
When two collections from multiple databases have the same name, forest admin just uses the first one.

This is because, in our code we index the collections using the mongoose model name.

Luckily, you can rename mongoose models without renaming the underlying mongo collection.

This is achieved by using the third parameter of the mongoose.model method.

module.exports = (mongoose, Mongoose) => {
  const schema = Mongoose.Schema({ ... });

  // mongoose model is 'actorsDb2'
  // underlying mongo collection is 'actors'
  return mongoose.model('actorsDb2', schema, 'actors');
};

Are both dbs in the same mongo server?
I’m not replicating this.

  • Can you check the PING_EXT_MONGO_NAME and PING_EXT_MONGO_URL? Both should be different (don’t use the same name for both dbs, and obviously, not the same url)
  • I see that you provided the options: createMongoConnectOptions() for the first db but not the other. Which flags did you pass to the working one, but not to the other?

Last but not least, if you are willing to upgrade, we have released a much more modern agent for NodeJS that does a lot to make multi-database setups easy to manage

1 Like

@anon39940173 thanks a lot for replicating my issue and for your advice.

I’m going to upgrade my version of Forest and then apply your recommandations.

I’ll let you know as soon as possible.

Thanks

@anon39940173 I ended by correctly connecting the two databases. First I had to solve the issue of naming models and collections and second I had to set the right authorization on my second DB to request it.

Thanks a lot

Best

that is great news, have a nice day