Multiple Databases with different authentication and a single MongoClient


Looking to move away from Mongoose to using the native driver directly in NodeJS.

We currently have a per database authentication - so each database has a different user / password - in Mongoose we connect each model using a different connection string - where the username and password is passed - along with the database.

Looking at the native driver example - the MongoClient is created once per replica set - so a cached client - but I am unsure then how to connect each database using a different username and password.

Are there any examples handy?

My current code for a test local instance of mongo looks like this:

const { MongoClient } = require("mongodb");

let tmMapsDB = null;
let staffDB = null;
module.exports = async () => {

  // Create a new MongoClient

  MongoClient.connect('mongodb://localhost:27017', function (err, client) {
    if (err) throw err;

    tmMapsDB = client.db('tmmaps')
    staffDB = client.db('staff')

module.exports.returnDB = (name) => {
  if (name === 'TMMAPS') {
    return tmMapsDB
  if (name === 'STAFF') {
    return staffDB
  return null

I should add some context - we typically have a single Node application which is responsible for a specific task - in this case the app is moving data from an old system (Domino) to multiple Mongo Daatabases on a scheduled basis.

It seems to me - either we have 1 application user which can access all databases or 1 node application per Database.

It seems odd that mongoose can manage this but not the native driver? Unless under the covers Mongoose is actually creating a lot of connections.