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 :
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)
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?
@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.