Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connect() vs createConnection()

Tags:

mongoose

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?

like image 650
desmondlee Avatar asked Nov 26 '16 11:11

desmondlee


People also ask

What is mongoose connect ()?

Mongoose lets you start using your models immediately, without waiting for mongoose to establish a connection to MongoDB. mongoose. connect('mongodb://localhost:27017/myapp'); const MyModel = mongoose.

How do I join two databases in MongoDB?

According to the fine manual, createConnection() can be used to connect to multiple databases. However, you need to create separate models for each connection/database: var conn = mongoose. createConnection('mongodb://localhost/testA'); var conn2 = mongoose.

Does mongoose close connection automatically?

You should close a mongoose connection when a Node POSIX signal is happening. SIGINT process is triggered when Ctrl-C has been pressed on terminal or a server shutdown. Another possible scenario is to close a connection when a data streaming is done.

What is useUnifiedTopology in mongoose?

serverSelectionTimeoutMS - With useUnifiedTopology , the MongoDB driver will try to find a server to send any given operation to, and keep retrying for serverSelectionTimeoutMS milliseconds. If not set, the MongoDB driver defaults to using 30000 (30 seconds).


1 Answers

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

like image 57
BenSower Avatar answered Sep 29 '22 12:09

BenSower