58

I am new to using mongoose and would like to know what is the fundamental difference between mongoose.connect() and mongoose.createConnection(), particularly in general what are the things to considered when using one over another.

My understanding on the official documentation is that generally when there is only one connection mongoose.connect() is use, whereas if there is multiple instance of connection mongoose.createConnection() is used.

Hope someone can clarify more about this.

Also, if my understanding is correct, what are the disadvantages of using mongoose.createConnection() in single connection? Why it is not advisable that we use mongoose.createConnection() for every case to standardize the connection?

desmondlee
  • 1,643
  • 4
  • 21
  • 33
  • 1
    I found some answers in http://stackoverflow.com/questions/22786374/queries-hang-when-using-mongoose-createconnection-vs-mongoose-connect – Ohad Navon Jan 26 '17 at 09:29
  • You can find more details here, https://github.com/Automattic/mongoose/issues/4700#issuecomment-260226027 – Kamalakannan J Jul 26 '18 at 18:03
  • [this video](https://www.youtube.com/watch?v=qYlfruqx_S8) is a good reference for connecting to multiple databases using `connect` & `createConnection`. Although it's in Hindi, you can follow along with the code in the video. – Gangula Nov 22 '22 at 18:39

1 Answers1

35

My understanding on the official documentation is that generally when there is only one connection mongoose.connect() is use, whereas if there is multiple instance of connection mongoose.createConnection() is used.

Yes. To be exact, .connect() creates actually a pool of sockets/connections (defined in poolSize in the connection settings, the default is 5) it keeps open, so there is actually multiple connections, but in a single pool. That being said, if you want multiple connections pools, with different properties, you should use createConnection

Also, if my understanding is correct, what are the disadvantages of using mongoose.createConnection() in single connection? Why it is not advisable that we use mongoose.createConnection() for every case to standardize the connection?

This is a good question and there's also already an answer in the docs:

Mongoose creates a default connection when you call mongoose.connect(). You can access the default connection using mongoose.connection.

Basically .connect is a shorthand for a set of (mostly) best practice settings for createConnection

In most simple projects, you needn't worry about specifying different read or write settings, pool sizes, separate connections to different replica servers etc., that's why .connect exists.

However, if you have more demanding requirements (e.g. for legal or performance reasons), you will probably have to use createConnection.

A couple of weeks ago, I ran into a situation, where one of my (in-house) statistics packages needed database access with a sporadic, yet big load. Since I didn't want to pass the db/mongoose object down to the package to keep it as modular as possible, I just created a new connection. This worked very well, because I only needed to access a certain Model I defined in the package and not the ones defined in my "parent"-package. Since the new package only needed read-access and could read from a different slave/replica db to reduce the load on the main one, I switched to createConnection on both ends to separate the connection from the main one.

For me, the big disadvantage of creating multiple connections in the same module, is the fact that you can not directly access your models through mongoose.model, if they were defined "in" different connections. This answer details that problem: https://stackoverflow.com/a/22838614/2856218

BenSower
  • 1,532
  • 1
  • 13
  • 26
  • came to your same problem recently as well. but with dealing with needing to connect to more than one domain, ie 2 clusters behind distinct domains. Mongoose and MongoDB seem to be designed around a single cluster connection per process. Which lead me to test out different approaches to manage this, ie, clustering node processes, or running a microservice arch just to connect these two clusters to a single branch of my API. Have you encountered this problem before by any chance? – jlmurph May 30 '19 at 14:48
  • I haven't had this exact problem before and I am not sure if I understood correctly, but what stops you from using createConnection like this: https://stackoverflow.com/a/19475270/2856218 , only with cluster-urls? https://mongoosejs.com/docs/connections.html#mongos_connections – BenSower May 31 '19 at 08:25
  • @BenSower But that means you have to pass the conn and conn2 objects when creating a model, right? So everytime you create a new object, you have to do = new MyModelA(conn) or new MyModelA(conn2) – Francis Zabala May 31 '19 at 09:10
  • @FrancisZabala: Yes and no. Yes, you have to tell mongoose on which connection this model should be "registered". No, you don't pass it to the model, but create the model on the connection like this: var ModelB = yourConnection.model('Model', new mongoose.Schema({ title :String })); As mentioned in my original comment, using mongoose.model(), is just a shorthand for registering it on the default connection created by mongoose. You can probably also do mongoose.connection.model(). – BenSower May 31 '19 at 10:06
  • What do you mean by 'object'? A model or a document? Everytime you create/register a model, you have to do it that way, yes. When creating a document, you can just directly call the Model like new Model({title: 'someTitle'}). Since the model only exists in the scope of a specific connection, it'll also only be saved there. – BenSower May 31 '19 at 11:14
  • Thank you @BenSower. I had to make the codes work before commenting. I apologize for my confusing comment earlier. I have deleted that. I was able to make the code work using the one you suggested. Thanks again! – Francis Zabala May 31 '19 at 11:30
  • Glad I could help! :-) – BenSower Jun 03 '19 at 10:18