'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



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steven Spungin