Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8149321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:49:37+00:00 2026-06-06T14:49:37+00:00

I am about to build my node.js / express / mongoose / passport application

  • 0

I am about to build my node.js / express / mongoose / passport application and I am thinking about the right schema design for users and accounts.

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

I can not think of a good solution for that situation. Here are the options I am thinking of:

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 nested account document.

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

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

2.Having all data in a single model

var ProfileSchema = new Schema({
    firstname: String,
    lastname: String,
    email: String,        
    twitter-uid: String,
    facebook-uid: String
    password: String
});

Well this is just ugly -.- It might be easier/faster to find the right account data but it’s not nice to maintain.

Is there a better solution? Is there a best practice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:49:38+00:00Added an answer on June 6, 2026 at 2:49 pm

    1) There are three strategies that you might take to structure your data in MongoDB:

    • a) Array of embedded documents
    • b) Array of embedded references
    • c) Expanded into the parent document

    Strategy (a) is the first one you describe, where the Profile document contains an array of Account sub-documents.

    Strategy (b) is similar to strategy (a), but you’d use an array of references to other documents (typically in an Account collection) rather than embedding the actual documents.

    Strategy (c) is the one you describe as “having all data in a single model”.

    2) It’s generally considered Best Practice to use an array of embedded documents, especially if the information in them can vary. If it will make your life easier, you can use a key to distinguish the type of the account, like so:

      { 
        firstname: 'Fred',
        lastname: 'Rogers',
        email: 'fred.rogers@example.com',
    
        accounts: [
                 { kind: 'facebook',
                   uid: 'fred.rogers'
                 },
                 { kind: 'internal',
                   username: 'frogers',
                   password: '5d41402abc4b2a76b9719d911017c592'
                 },
                 { kind: 'twitter',
                   uid: 'fredr'
                 }
              ]
        }
    

    3) MongoDB allows you search on an embedded document. So you would write the following query (JavaScript syntax):

     db.profile.find( 
            { email: 'fred.rogers@example.com', 'accounts.kind': 'facebook' }
            );
    

    With appropriate indexes, this query will be quite fast.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am thinking about build a online store for my wife to sale something.
I am thinking about how to build advertise site which works like twitter. That
I'm about to take the plunge and build a PC, and it will probably
Just thinking about the best way to build an Order form that would (from
Are there tools that will profile the Maven build process itself so that I
I am about to build a web application that shares resources through a Restful
I have multiple Node applications (build on Express framework). Now I have placed them
I'm about to build an app that has a database of places that has
At my company we're about to build a new site using ASP.NET MVC. My
I have a quick question about altering the build path as the code is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.