简体   繁体   中英

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:

  1. merchant_id to database name. (Which merchant is assigned which db as each merchant should have separate db)

  2. 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:

  1. 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).

  2. 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.

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");
});

您应该能够使用单个文档和最新的MongoDB Stitch解决问题,其中我们拥有字段级访问控制。.我建议您使用Stitch功能一次

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM