How to reuse MongoClient in Mongoose? - node.js

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?

Related

Mongo Atlas Data API and working with Mongoose Schemas

Hi we are looking into migrating our current mongo logic which uses mongoose schemas and the driver to conduct CRUD functions. We run this on serverless functions with many microservices that connect to the db so we have been hitting many issues with connection pool max limits.
We have decided to migrate to the Data API because it would resolve our connection issues. One issue we dont understand or can find documentation on is how do we leverage mongoose schema with the data api?

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

Mongoose Schema.post across network

I have two Node.js applications each running Mongoose on a different machine. There is a single MongoDB database running on the first, and the second connects to it and adds documents periodically. I'm trying to add a hook to the creation of these documents so the server running the database is aware that other server has added data. I tried using the Schema.post() method, but it doesn't seem to work since there are two separate instances of Mongoose. Is this true or am I just implementing it incorrectly? I can get the hook to fire if the document is created on the same server, but not the other.
So my thought is to add the hook to MongoDB directly, instead of Mongoose, but I'm not sure how to go about doing that. Am I on the right track?
That is true, Schema.post() only work in the same process. You either need to use a library that tails MongoDB's oplog (like mongo-oplog), implement it yourself using a message queue (or pub/sub) that all instances are connected to (like Redis, RabbitMQ, etc) or use a database that supports this natively. PostgreSQL supports this with its NOTIFY feature for example.

NodeJS - Best way to inherit mongo connection

I'm working on a NodeJS package which works with mongodb using mongoose.
What I want to achieve is that you won't need to pass any connection "config" on init if you know you have already invoked db connection.
Is there some way that my package could use already existing connection?
Or am I missing something, is there some better way for that ?
You create mongoose instance once and make references to it (mongoose = require('mongoose'). So if this instance is connected to DB you no longer need to state connection again.

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.

Resources