MongoDB, mongoosejs - using multiple DBs? - node.js

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?

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 access a MongoDB database from a different Server

In a microservice architecture, I have 2 separate services with different mongoDb databases. Teacher-Service and Student-service.
Now I'm trying to create a login function, once a user submits an email I'd love to query both databases to determine if the user is either a teacher or a student.
How do I implement this in Node.js using the express framework.
I guess you can make HTTP requests to both services (synchronous communication).
For e.g. GET http://teachers.service.com/api/users?email=<input-email> and GET http://students.service.com/api/users?email=<input-email> with some auth header with mind of course :).
Or another way to communicate through services is using asynchronous communication transporters like RabbitMQ, Kafka, NATS and so on. You can search for their documentations to get ground up.

Connect to multiple Mongodb Databases

I want to add data to different databases of mongodb at runtime depending on the request. How can i connect to multiple databases in nodejs at runtime?
For MongoDB, IN NodeJS, you cannot connect to multiple databases at once,
But I suppose you want to add data to different databases, for that you do like this article :-
https://medium.com/#rajamanii/connecting-multiple-database-in-nodejs-with-mongodb-and-mongoose-d88574fcd5a3

Loopback with meteor

we have a database connected to meteor and loopback services each one edits in the same collection in database.
I found that meteor have to create the model in a way and this link in loopback https://loopback.io/doc/en/lb2/MongoDB-connector.html
includes only connecting to database and create model not to connect already with a collection in database so anyone have an idea how to design and connect the two services with same collection to edit ? or they detect automatically the collections once I connect the datasource?
Yes, this should already work. Meteor uses OPLog tailing to detect changes to the database and it doesn't have any opinion on how the documents in the collection should look like. It will create _ids that are strings rather than ObjectIds, but it can work with either. So as long as the other services doesn't mind the string _ids you should be fine.

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