title: MongoDB schema design for multiple auth user accounts
body: <p>I am about to build my node.js / express / <strong>mongoose</strong> / <strong>passport</strong> application and I am thinking about the right <strong>schema design</strong> for users and accounts. </p>
  
  <p><strong>There will be users loging in from twitter and facebook as well as from native accounts. At a later stage I want a user to connect both twitter and facebook with my application (and maybe even more external accounts).</strong></p>
  
  <p>I can not think of a good solution for that situation. Here are the options I am thinking of:</p>
  
  <p>1.Having a profile model and account models. Profile documents represent the unique user, while an account provides either username and password (internal account) or the authentication data from the auth-provider (external account). A profile has to have at least one <strong>nested</strong> account document.</p>
  
  <pre><code>var ExtAccountSchema = new Schema({
      type: String, // eg. twitter, facebook, native
      uid: String
  });
  
  var IntAccountSchema = new Schema({
      username: String,
      password: String
  });
  
  var ProfileSchema = new Schema({
      firstname: String,
      lastname: String,
      email: String,
      accounts: [Account] // Pushing all the accounts in there
  });
  </code></pre>
  
  <p>What I dislike about it are the not so consistent account documents resulting from different account data and the fact that I have a hard time finding the right account when my user logs in (searching uids and account types in nested documents -.-)</p>
  
  <p>2.Having all data in <strong>a single model</strong></p>
  
  <pre><code>var ProfileSchema = new Schema({
      firstname: String,
      lastname: String,
      email: String,        
      twitter-uid: String,
      facebook-uid: String
      password: String
  });
  </code></pre>
  
  <p>Well this is just ugly -.- It might be easier/faster to find the right account data but it's not nice to maintain. </p>
  
  <p>Is there a better solution? <strong>Is there a best practice?</strong></p>