user13544979
user13544979

Reputation:

MongoDB design for multiple auth user accounts

I have a backend with nodeJS, Express and MongoDB / mongoose...

My users can signUp/Login with Google or email and password. But now my question is what's the best option of building a model for that.

My first option is this:

{
    firstname: myname,
    lastname: mylastname',
    email: '[email protected]',
    accounts: [
         { provider: google,
           idGoogle: myid, 
         },
         { provider: simple,
           email: [email protected],
           password: myp4ssw0rd
         }
      ]
}

or:

    {
        firstname: myname,
        lastname: mylastname',
        email: '[email protected]',
        accounts: [
             { id: q1w2e3,provider:google},
             { id: r4t5y5, provider:simple},
          ]
    }

and a reference to another collection

    {
        _id:q1w2e3,
        provider:google
        idGoogle:wqwqe24},
    {
        id: r4t5y5, 
        provider:simple,
        email:[email protected]
        password: myp4ssw0rd
    }

My main goal is optimal performance with many users

Upvotes: 0

Views: 274

Answers (1)

Steven Spungin
Steven Spungin

Reputation: 29099

I would not use a separate collection and lookup. MongoDB will manage the embedded document without much overhead. And if it starts degrading, just add an index.

The simplicity of queries with the embedded document outweighs any overhead.

Upvotes: 2

Related Questions