title: Mongoose Multi Connection with Fastify
body: <p>I am trying to use multiple database connections in fastify with mongoose.</p>
  <p>for single db code looks like</p>
  <pre><code>const mongoose = require('mongoose');
  const fastifyPlugin = require('fastify-plugin')
  mongoose.Promise = Promise; // Set mongoose to use ES6 Promises.
  const reconnectTimeout = 5000; // ms.
  const db = mongoose.connection;
  
  async function dbConnector1(fastify, options) {
      const conn1 = mongoose.connect(&quot;mongodb://localhost/db1&quot;, {
          useNewUrlParser: true,
          useUnifiedTopology: true,
          retryWrites: false
      })
          .then(() =&gt; console.log( 'Connection to DB1 successful'))
          .catch((err) =&gt; console.error(err));
  };
  
  module.exports = fastifyPlugin(dbConnector1,{
      name: 'DB1'
  })
  </code></pre>
  <p>I have put the above code in conn1.js
  I have created a similar connection with different db in conn2.js and in index.js I am registering the db plugin as follows</p>
  <pre><code>        await fastify.register(require('./src/db/conn1'))
          await fastify.register(require('./src/db/conn2'))
  </code></pre>
  <p>I get the following error -</p>
  <p><strong>MongooseError: Can't call <code>openUri()</code> on an active connection with different connection strings. Make sure you aren't calling <code>mongoose.connect()</code> multiple times. See: <a href="https://mongoosejs.com/docs/connections.html#multiple_connections" rel="nofollow noreferrer">https://mongoosejs.com/docs/connections.html#multiple_connections</a></strong></p>
  <p><a href="https://i.stack.imgur.com/lI7wK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lI7wK.png" alt="enter image description here" /></a></p>