MONGOOSE MULTIPLE CONNECTION
Tags: mongodb, mongoose, connection
Published: 2021-06-05 06:57:08

Mongoose connection is a class representing a collection of sockets that connects to one or more MongoDB server process.
```javascript
    const mongoose = require("mongoose");
    const conn = mongoose.createConnection('mongodb://localhost:27017/test', {useNewUrlParser: true, poolSize: 10});
//create a model using the custom connection conn created
conn.model("myModel", new mongoose.Schema({}));
```
The created connection object conn above uses a custom mongoose connection instead of the default one. From the sample connection above we pass a poolSize option, which is set to 10. The poolSize is the number of concurrent connection that can be made to the database server. The default mongoose connection has a poolSize of 5.
```javascript
    const mongoose = require("mongoose");
    mongoose.model("myModel", new mongoose.Schema({}));
//this default connection has a poolSize of 5. 
```
### Multiple Mongoose Connection
Mongoose default connection works for most apps but there are instances when you will want to connect to more than databases in your application. At such times the default mongoose single connection no longer meets your needs.
Some instances why you might require multiple connections:
1. you have more than one databases to create models on and connect to
2. your apps have to perform some aggregations or run some slow queries. To prevent slowing down your application data access, another connection can be created for that slow query or aggregation work so that one connection handles the slow query while another connection will be for the normal run of the application.

### Setting Up Multiple Connections
Assuming we have the following folder structure in our application:
- Connection
   - fast.js
   - slow.js
- Schema
   - userSchema.js
   - index.js
Using the Schema export pattern where we export our Schema.
```javascript
   //content of the userSchema.js file
  const mongoose = require("mongoose");
  const schema = mongoose.Schema;
  const userSchema = new mongoose.Schema({
     username: String,
     password: String
});
export default userSchema;
```

```javascript
   //content of the index.js file. 
  const userSchema from "./userSchema";
  const mongoose = require("mongoose");
  export default (db) => {
    //db here represents the connection object that will come 
     from our connection file
    db.model("User", userSchema);
    //other file in our schema
    //db.model("model", modelSchema) 
   
    return db;
}
  
```


```javascript
   //content of the fast.js file. 
   const modelSchema = require("../Schema");
const mongoose = require("mongoose");
   //modelSchema represents the schema which we exported from  the index.js file in the Schema folder
 export default () => {
   const conn = mongoose.createConnection(uri, {useNewUrlParser: true, poolSize: 10 });
return modelSchema(conn);
}


```
To make use of any of our connection object we just require the connection file
```javascript
  const fastConnection = require("./connections/fast");
  const slowConnection = require("./connections/slow");
 //create our connection object using the connection.
 const fastConn = fastConnection();
 //to make use of the fastConn
 fastConn.models.User.find({});
//we can now make use of any of the connections in our application here
```
Happy coding...