Connecting Multiple database in the predefined Mongoose structure

Clash Royale CLAN TAG#URR8PPP
Connecting Multiple database in the predefined Mongoose structure
First of all, I will give you a glimpse on our project:
We have an application, which has two user roles: Merchant and buyer.
A merchant signs up and stores his trade info in his account.
A buyer can also log into his own account and access his purchase info with that merchant. One buyer may deal with multiple merchants.
We are thinking of a mongo database structure where the universal mongo db has only two key value pairs:
merchant_id to database name. (Which merchant is assigned which db as each merchant should have separate db)
buyer_id to merchant ids. (say buyer Ram has relations with two merchants M01 and M02).
We want that when each merchant logs in, he should see only data from his db.
But when a buyer logs in, he should see data from the database of merchants with whom he has business relations. (Ram should see data pulled from the database of M01 and M02).
We want a separate database for each merchant who signs up on our portal. (Since each merchant has lots of transactions). We had defined all the functions such as calculations and all the logic part with the help of mongoose ORM. As we have defined a global db.js and then with the help of model, schema and routes we are using the mongoose connection request with the database.
Following are our problems:
When our server starts, it connects to the database that is defined in the db.js file and in that there is a connection request to a global database or we say a master database.
How can we connect more then one database, while remaining connected to the master database? (closing up the connection and connecting to a new database causes us to lose connectivity with the master database [Which has all our signup data] and hence is not a solution).
We want to make a new database for every merchant after that merchant signs up, based on some key (Like IDCardNo) a new separate database is created, and all the calculations and the logic part are being then defined dynamically which performs with that particular database.
One thing is that we have used mongo-client instead of mongoose to make a new database, but got stuck as we wanted to make all the master database calculations and logic part (that are being written earlier) to being dynamically operated. Our problem is: How can we define more than one database connection request to that db.js file, which is static in nature, and also all our logic and calculations part are being written on the basis of that mongoose connection and not as a separate function?
I just found a solution on the internet that makes different folders for each user having mongoose and also the node_modules folder separately, but we cannot do that here as there are so many connection requests which hinders the server performance and also the storage load will increase.
I found another solution: change mongoose to mongo-client. This solution is very expensive since it means that we have to redo many critical pieces of code-similar to making the product again.
I would request you to help us to solve this issue and architect this system.
3 Answers
3
You can connect to the multiple databases with the same models by:
var mongoose = require('mongoose')
var conn = mongoose.createConnection('mongodb://localhost/db1');
var conn2 = mongoose.createConnection('mongodb://localhost/db2');
var Schema = new mongoose.Schema()
var model1 = conn.model('User', Schema);
var model2 = conn2.model('Item', Schema);
model1.find(, function()
console.log("this will print out last");
);
model2.find(, function()
console.log("this will print out first");
);
You should be able to solve this issue with single document and latest MongoDB Stitch wherein we’ve field level access control .. i recommend you go thru Stitch capabilities once
Do you have any link to learn these mongodb Stitch version. As i didn't get any links to that and also i am not getting any reference with the above question in this version
– Shubhankar Dubey
Aug 16 at 12:07
Maybe Instead of creating 2 separate databases for the merchant and buyer, you should define 2 mongoose schema or Collections one for the merchant, one for the buyer.
Merchant Schema
const mongoose = require('mongoose');
const schema = mongoose.Schema;
const merchantSchema = new schema(
// merchant details
)
mongoose.model('merchant',merchantSchema )
Buyer Schema
const mongoose = require('mongoose');
const schema = mongoose.Schema;
const buyerSchema = new schema(
// merchant will store merchant_id(merchant_id is unique id given
// by mongodb for every merchant you save in collection)
merchant: [
type: schema.Types.ObjectId
ref: 'merchant'
]
// buyer details
)
mongoose.model('buyer',buyerSchema)
As you mentioned, "merchant should see data only from his db(here collection)".
To show data to a particular merchant you can query merchant model.
Eg:
merchantSchema.findOne(_id: merchant_id)
And to show data to user of merchants with whom he has business relations you can use
buyerSchema.findOne(_id: user_id).populate('merchant')
This will solve your problem I guess.
For a useful answer this reaction needs to be extended. Format your code example and explain why this is an answer to the question.
– Jeroen Heier
Aug 13 at 16:26
we can't use different collections for different merchant and buyer, because each merchant has data in 2 collections, and each buyer has 8 collections more. So it is not feasible to have a database with 1000+ of collections.
– Shubhankar Dubey
Aug 14 at 12:50
I am searching for the method through which we can divide our database separately for each merchant which contains its own data plus the related buyer data.
– Shubhankar Dubey
Aug 14 at 12:52
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
My issue is still not resolved. My issue was : When our server starts, it connects to the database that is defined in the db.js file and in that there is a connection request to a global database or we say a master database. How can we connect more then one database, while remaining connected to the master database? (closing up the connection and connecting to a new database causes us to lose connectivity with the master database [Which has all our signup data] and hence is not a solution).
– Shubhankar Dubey
Aug 8 at 11:25