Question about mongo vs mongoose

I have been reading a lot of the documents about mongo and mongoose. I do prefer to use mongoose and understand that a lot better. One thing that i have a hard time wrapping my head around is the connections.

To me, it looks like with mongoose, you establish a connection with the server.js file and leave it open.

With mongo, it seems you use an instance of mongoclient on each http call and then close the connection on each the end of the call.

Am i just reading this incorrect.? Can both operate either way? This maybe comes from me being less familiar and just learning databases. Maybe i should not be connecting mongoose on sever and not leaving it connected indefinitely?

Hello there,

In general, it is expected a server will only connect to a DB once, and persist the connection as needed. This is use-case dependant, and I know some applications close the connection relatively often.

So, none of this is Mongo Driver vs Mongoosejs specific.

Most applications (web) do remain connected for the life of the server.

Hope this helps

1 Like

This isn’t neccessarily true.

Mongoose uses mongodb libraries internally, so it provides the same API, but with features ontop for schema definitions, so you get some structure to your data.

The underlying mongo library usually doesn’t care/provide much more than the basic apis, which again are also available with mongoose.

As said above the re-use of a connection isn’t specific to mongoose vs mongo, you could keep the same connection or close+re-open them. However the one area to keep in mind is mongoose also will re-apply schema definitions on startup and so stuff like updating database indexes, this has some performance implications which need to be considered whenever you open a new connection.

1 Like

Thank you. That clears things up some for me