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
Related
I'm developing a chat application that consists of several micro-services or nodes. And to handle database I use Sequelize ORM.
However, I've two separate nodes one for handling socket messages and another one is a general API server. Both of them have to use Sequelize.
How can I use Sequelize in both? I don't want the same code to copy paste in two different services.
What we have done in our project, is to connect socket server and API server. Thus API server is a special socket client for the socket server. Thus if we need to do some db query we pass data to-fro between them.
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.
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?
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.
I have nodejs application with express. I store my session in mongodb. But also I store application data that I store in the same db. Should I create another mongodb database for a sessions?
You can use the same database, but have different collections (a collection in MongoDB is somehow similar with what MySQL calls table). That would be ok too.