5

I am using mongodb and mongoose to handle my database needs. I currently have this:

mongoose.connect(secrets.db, {server:{poolSize: 1}});
mongoose.connection.on('error', function() {
  console.error('MongoDB Connection Error. Make sure MongoDB is running.');
});

As my mongo connection. I want to change that so that each user that logs in (using passport) gets their own database. So when I do show dbs I would like to see user_ObjectID_database_name how can I modify my code to get this result? I want it because I believe it is more secure from my research. I can not however find any good guides on how to do this. I am sure they exist but I have spent 4 ish hours looking with no luck on even a semi-okay one that I could guess at.

Currents I just have a users collection.

This is my current code that runs when a user hit /signup as a POST request.

exports.postSignup = function(req, res, next){
  req.assert('email', 'Please sign up with a valid email.').isEmail();
  req.assert('password', 'Password must be at least 6 characters long').len(6);

  var errors = req.validationErrors();

  if (errors) {
    req.flash('errors', errors);
    req.flash('form', {
      email: req.body.email
    });
    return res.redirect('/signup');
  }
  // calls next middleware to authenticate with passport
  passport.authenticate('signup', {
    successRedirect: '/dashboard', // Select redirect for post signup
    failureRedirect: '/signup',
    failureFlash : true
  })(req, res, next);
    next();
};

I call this as a middleware in my route to keep them cleaner.

EDIT **

Would sharding be a better solution for performance? I know it requires a lot of set up and planning but it might be worth it? Only question is security...

8
  • Possible duplicate of Mongoose and multiple database in single node.js project Commented Nov 2, 2017 at 2:02
  • See the suggestions about mongooses useDb in that answer Commented Nov 2, 2017 at 2:03
  • @Matt That is not what I am looking for, I am looking to base it on user accounts not applications. This is one application with 1000+ users. What you linked to would work if I had say 2 separate applications. Commented Nov 2, 2017 at 2:16
  • 1
    What you are looking to do and what you can do don't change the fact that your stated design requires multiple mongoose databases in a single node.js project. Maybe if we go a step back from this, What problem are you trying to solve with a database per user? A performance concern? Managing an application and a mongo instance with 1000+ databases sounds like trouble. Commented Nov 2, 2017 at 2:49
  • 1
    @Kirbytech Your app is still the gatekeeper between users data, wether it's in multiple databases or separated by where clauses. Performance wise, I guess you could manually shard users to different hosts as needed with seperate db's if you wrote the lookup system. Otherwise collections will cover an index/data size issues you come into. Commented Nov 3, 2017 at 0:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.