I have a requirement where I have to connect two different databases on different remote servers with mongoose from my main server.
Now,I have done a bit of research and founded that we can connect multiple instance with mongoose and then can use useDb()
to use the specific database.
Everything works fine when I am connecting it to 2 local database but when the database are remote and I am connecting it via IP it is giving authentication error.
I guess connections are getting mixed.
Below is my code that I am using which is working on local database:
mongoose.connect('mongodb://'+db.host+':'+db.port+'/'+db.dbname,{user: db.username, pass: db.password, useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true, useFindAndModify:false })
.then(() => {
console.log(`db connection successful`);
mongoose.connect('mongodb://'+db2.host+':'+db2.port+'/'+db2.dbname,{user: db2.username, pass: db2.password, useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true, useFindAndModify:false })
.then(() => console.log(`db connection2 successful`))
.catch((err) => logger.error("db connection2 error", err));
}).catch((err) => logger.error("db connection1 error", err));
When host is localhost everything works fine but when I change it with the IP of the server it throws authentication error
.
'not authorized on db to execute command { find: "collection_name", filter: { basicDetails.applicationNumber: "5000" }, projection: {}, returnKey: false, showRecordId: false, lsid: { id: UUID("164bf515-cec6-470f-fewcfrv") }, $clusterTime: { clusterTime: Timestamp(158461230, 1), signature: { hash: BinData(0, 6210C6E1586CFDBDC), keyId: 12346890 } }, $db: "DB" }'