How many connections should I open to mongodb? - node.js

If I using in my app for example - 3 databases, should i need to open connection for each one, or one connection for all?
I using mongodb driver in nodejs.

Related

MonogDB connection opening closing and caching when to use them correctly?

I'm new to the database world (learing mongodb for node)
I'm a little bit confused about the client connection, I saw mongoDB connection doc example creating connection and close it but I'm not sure why they close it.
I'm thinking and asking myself if my end user will send to my server (nextjs) and the server will open a connection - at least the first time the server is up - to mongodb and fetch the data then send it back to the user.
in such a case should I close the connection (next users could use the same one)?
if no. which cases I have to close it ?
another confusing part to me about connection is caching :
I saw some code examples that do caching connection while I read that mongo has some sort of built in caching in RAM . is there a different ?

Using Node.js and Mongoose: Is switching between multiple connections on a MongoDB server an inefficient alternative?

I have several clients. Each client has multiple data acquisition stations. Each station has multiple sensors. I need to store this data on a MongoDB server (using mongoose and Node.js). So, I thought about the organization that follows.
Each client has its own database inside the MongoDB server.
Within this database, each station has its own collection.
Data is sent to the MongoDB server via an MQTT broker (Node.js). So, depending on which client the broker receives the message from, I need to create a new connection to the MongoDB server (using mongoose.createConnection).
I'm not sure if this is the best alternative. I don't know if creating multiple different connections will slow down the system.
What do you think?

how to access various mongo databases with node

I need some help with this question.
I try to build an node express REST api which has to deal with various mongoDB databases on the same mongoDB server.
What is the right approach to do this?
The sequence could be:
app starts
connect to the mongoDB Server
use the right express route
check which database is needed
finally query the correct data
connection to the database is still open
OR
use the right express route
connect to the mongoDB Server
check which database is needed
query the data
close the DB connection
AND how can I do this with mongo-native driver, not mongoose?
Thanks for any help.
In Mongo, database connections are persistent - that is you should leave a connection open and not close it until you want to close your server.
Assuming you're using the new Mongo driver (new as in, not 3 years old), it will handle reconnects and managing the connections for you - all you have to do is just connect to it once the server starts and close the connection when the server ends.
This is a property of the node driver - so it is equally true for the driver itself and wrappers/mappers like Mongoose.

Sharing a DB connection on the entire app or connect to DB on each request?

I'm doing an API with Nodejs using Restify.
For the DB I'm using Mongodb (with mongoose).
I was wondering, what the best solution between sharing a db connection to my entire app or connecting to the db on each request ?
For now, I'm using the second option of this answer : sharing db connection
But I've seen a different pattern here : Node.js Web Application with Storage on MongoDB
I can't figure out, what is the best architecture ?
A list of pros and cons could be a great help.
Of course keeping one connection ( or pool of connections if mongoose supports it ) and reusing it is better, simply because creating connection on each request eats resources.

mongodb in node.js connections today

When using the native mongo.db driver for node, should I open 1 connection per application, per page "serve", or open and close it whenever I need?
I've seen a few older answers but I know the project is always developing so I want to know what it the status today.
This isn't a situation that will change; opening a new connection to a server will be less performant than using an established connection.
Note: this is the general case for server applications, and not specific to MongoDB.
Typical overhead includes:
resolving server names to IPs
establishing network connection to server
per connection memory allocated on the server
For MongoDB in particular:
opening a new connection means a new socket connection and thread on the server
each connection (as of MongoDB 2.0) allocates 1Mb of RAM on the server (see also: Checking Memory Usage)
there is a per process limit on open files/connections (see also: Too Many Open Files)
For the MongoDB Node.js driver you can take advantage of connection pooling by setting the poolSize in the constructor. A blog post with an example of using this: Node.js: Connection Pools and MongoDB.

Resources