How to configure Mongoose with an already existing Mongo connection - node.js

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

Related

How to reuse MongoClient in Mongoose?

I have a NodeJS application which uses MongoDB native driver to perform database operations. I have already initialized MongoClient. I would like to implement new features using Mongoose. How I can make Mongoose instances use already initialized MongoDB client? I searched the docs https://mongoosejs.com/docs/connections.html and I can't find anything saying about setting the client. I found only information about getting it. Is it possible at all?

use custom mongoDB server instead of mongolab

Today i started migrating from firebase to mongoDB,
I used this tutorial and its all up and running http://thejackalofjavascript.com/re-architecting-a-firebase-app-in-node/
When checking the code i see there is a mongolab link connected to the code,
mongodb://admin:admin123#ds061620.mongolab.com:61620/testsync
My question is: can i easily setup my own local database to use instead of this? and what packages i would need to do this?
The main reason for switching to mongo instead of firebase is the pricing, please take this into account.
Yes, of course you can.
First, install mongodb locally, to do that, follow the instructions for the distribution you're working on.
Then, make sure your mongodb service is running.
After that, on your database connection script, change the connection parameters.
I guess that you have something like this on your .js file:
mongodb://admin:admin123#ds061620.mongolab.com:61620/testsync
Just to make a connection test, try to change it to:
mongo://localhost/test
After a successful connection, you can start to manage your database as you want.
As additional information, you don't have to specify user, password and a different port than the mongo's default, because you're using your local configuration, if you want to do so, you have to configure your mongodb server to make it work that way.
I'm working with mongoose ORM to manage a local database, and here's my connection function working.
Mongoose connection to local database
Hope this helps you.

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.

is it safe switching db on mongoose?

in my application I have a default database and other database I have to connect to in function of client's requests , since with mongoose in node as far as I understood: there is a pool of connections application wide, if I change database, it is changed for all the subsequent requests, I think it could cause some problems, what is the best way to switch Database with mongoose?
Mongoose 3.7.1 (unstable) supports switching databases.
Otherwise you'll need to create separate connection instances for each database.

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