I am trying to understand the Connection.prototype.useDb() method better. I would appreciate it if someone could help me.
1. The mongoose document for useDb() states:
Switches to a different database using the same connection pool.
Does this mean that if there is an available connection in the connection pool useDb('db1')
will reuse the connection? in other words can a connection used by useDb('db1')
, be reused by useDb('db2')
?
Or does the statement simply mean that useDb('db1')
will create a new connection in the connection pool and can only reuse connections already created by useDb('db1')
?
2. The mongoose document for useDb() states:
[options.useCache=false] «Boolean» If true, cache results so calling useDb() multiple times with the same name only creates 1 connection object.
What happens in the following scenario(assuming that maxIdleTimeMS
equals 0 which means no limit and maxPoolSize equals 2)?
3.
Considering:
[options.useCache=false] «Boolean» If true, cache results so calling useDb() multiple times with the same name only creates 1 connection object.
and considering this from mongodb.com which states:
MongoDB is synchronous and uses a single execution thread per socket, meaning that MongoDB will execute one single operation per socket at any point in time. Any other operation sent to that socket will have to wait until the current operation is finished.
Does this mean 5 queries which are supposed to be executed on mydb2
, using the connection from conn.useDb('mydb2', { useCache: true })
, would have to wait in queue to be executed one after another? as opposed to conn.useDb('mydb2', { useCache: false })
, which means they will be executed on separate connections and in parallel?
EDIT
Since mongoose uses Nodejs mongo driver's db method to implement its useDb
method, and since the documentation for db()
method in mongo driver states:
Create a new Db instance sharing the current socket connections.
I guess this means that each connection(socket) in the connection pool can be shared regardless whether they were created by calling conn.useDb('mydb1')
or conn.useDb('mydb2')