how to access various mongo databases with node - node.js

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.

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 ?

How many connections should I open to mongodb?

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.

How to configure Mongoose with an already existing Mongo connection

I have an app that is already working with the native Node Mongo driver (v3.0).
I'm now trying to slowly implement Mongoose in order to make the app easier to maintain. I would like to do this in a gradual way so I rewrote all the user related operations with Mongoose and the rest like it was before. I noticed that my app now creates two connections to my Mongo db. This is clearly because Mongoose knows nothing about my existing connection.
I would like to handle connecting and disconnecting to Mongo myself and give Mongoose a reference to the already existing connection but I can't find anything like this in the docs.
Is this even possible or will I need two different connections until my app is fully rewritten to use Mongoose exclusively?
EDIT: My app is being run as an AWS Lambda function which has to connect and disconnect to mongo on every request so having two concurrent connections per request is effectively halving my mongo db available connections. That’s why I’m concerned about having an extra connection.
Turns out the answer to this is to do it the other way around. Just connect to Mongoose and then grab the connection.
let mongoConnection = mongoose.connection.client

Sails mongodb missing connection handling

Very simple and stupid question I came up with today when was playing around with sails and mongo db (adapter of the waterline odm). I just shut down the mongo while my sails server still has been running and saw nothing, neither in logs of sails or in the browser. How I could handle such situation?
If sails.js attempts a connection to the DB you will most certainly get a message in your console if the Mongo server is shut down, but if no request is made that requires a DB connection, then you will not see any error because no error as of yet exists.
It sounds like you might require a process that will monitor your DB and check to make sure it is still running? There are Sail.js options where you could create a CRON job to check the connection. Or you could use other application monitoring services like New Relic to monitor your DB.

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.

Resources