Only use 1 connection in multiple files

Hey. I’m working on a backend system that includes multiple node.js files. It means that in each of the files using MongoDB, I have to open a new connection using MongoClient.connect().

But the thing is, that MongoDB doesn’t really seem to like it: I got multiple deprecation errors in the console:

the options [servers] is not supported
the options [caseTranslate] is not supported
the options [dbName] is not supported
the options [srvHost] is not supported
the options [credentials] is not supported
[sometimes more]

So my question is: How can I open only 1 connection, so I can use it in all my files? Since the code looks like that

MongoClient.connect((err) => { // err handling
    // code using the database
});

not like that

MongoClient.connect((err) => /* err handling */)
// code using the database

I can’t figure out how to find an answer.

Thanks in advance for your help.

Hi @a2b,
What is placed as the connection string for this client?

What is the minPoolSize and maxPoolSize? They should be both set to 1.

Best
Pavel

The issue has been solved with this StackOverflow question.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.