Mongo Atlas Data API and working with Mongoose Schemas - node.js

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?

Related

How can I implement MongoDB on Angular 9?

I'd like to know how to implement MongoDB to Angular 9 for Inserting, Deleting and updating data. I heard it can be done using Node JS.
I need some sample code for an academic project.
This link is a great step towards learning NodeJS express and Mongo DB connection with CRUD operations.
The author has mentioned step by step procedure for DB operation with Node.
And to combine it with Angular, you can create something to call CRUD APIs of your Node application which will handle the DB connection.

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?

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

MongoDB, mongoosejs - using multiple DBs?

My backend is NodeJS and uses 2 DBs (MongoDB) with mongoose. One of them has a Users collection and the other db is used for a messaging service that uses user ids.
Does .populate() work across DBs? If so, how is that done?
Would mirroring the users in both DB, where the messaging one would only have a subset of fields for the users, be an a good option?
Also, is it worth having 2 DBs worth it for scalability?

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