I am working towards implementing a setup in mongoDB where each user has their own table (collection of documents). How can I dynamically switch between tables during runtime, based on the user who is logged in? I currently have this code snippet...
var mongoose = require('mongoose'), Schema = mongoose.Schema;
var TicketSchema = new Schema({
Unit_descriptor: String,
...
Total_tons: Number
}, { strict: false });
module.exports = mongoose.model('tickets_user1', TicketSchema);
...and my goal is to be able to switch the table for the current user, perhaps by appending the username as a suffix to the table name.
I attempted to create an exported function that could be called later from the controller to swap the table, like so:
var auth = require('../../auth/auth.service.js');
...
module.exports.initTable = function initTable() {
module.exports = mongoose.model('tickets_user2', TicketSchema);
};
However, this approach did not result in the tables being swapped, and data for user1 is still present. Can someone provide guidance on how I can accomplish this? Thank you.