I am using mongoose in one of my projects. So when I start the server I connect to mongodb using mongoose.connect(url)
I have a 3 questions here
Does mongodb closes the connection after a certain amount of time if the connection is not used?
Will mongoose automatically reconnect if the connection gets closes/disconnected?
How to make sure if any time connection gets closed, it will automatically reconnects?
I have seen some old question answers where they had a options object which contains how many time mongoose will try to reconnect if connection closes, but those options aren't supported right now.
Related
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 ?
When users hit multiple requests at the same time. The execution time of the api is taking lot of time and the mongoose connection is getting disconnected for apparently no reason and then later getting the error mongooseServerSelectionError timeout after 3000ms. How to stop the mongoose connection from getting disconnected?
From what I have read online the mongoose connection gets disconnected due to inactivity link. But I am not sure how to stop it from disconnecting. I tried setting keep-alive: true and keepAliveInitialDelay:10ms and also played around with connectTimeoutMS and socketTimeoutMS but none stopped the disconnection of the mongoose connection.
Please help me out here.
If I try to create a new connection and close the older one when the mongoose connection gets disconnected, what kind of impact does that have on the mongoDB transactions and the sessions that are currently ongoing?
Edit: The existing mongoose connections are disconnecting when the no of tcp/http connections are high/ we are internally calling external api's. So how to stop the existing mongoose connection from disconnecting? Is there any hard limit on nodejs that we cannot use more than x no of connections open at a time?
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
I'm using mongoose to maintain a connection with the database for my node.js app.
What I am doing is essentially open a connection the the database when the app starts and close the connection when it terminates. It's working fine and I have absolutely no issues querying and manipulating data, so no issues there.
I have a fair idea about how it works and how connections are pooled. However, I noticed that when the app is running, I randomly get an extra connection to my database - Multiple connections to DB
I'm using nodemon to track any file changes in my project but since the app closes every time nodemon cause it to restart, the connection should be destroyed because the app is closing, right?
Is it advised to close connections anywhere while the app is up and running and open it again as and when required? I haven't done that anywhere because from what I've read, it takes more time to open and close connections for every query. Could this be because I'm not disconnecting anywhere except for when the app closes?
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.