Most appropriate Mongo database schema layout? Voting App

Hi,

Just having a think about the Voting App backend project and the best way to layout the database. One option (Idea 1) would be to have the user and their polls in the same Mongo Collection, and Idea 2 would be to split the information into two collections; one for the users and one for the polls.

I’m not sure of the potential pros and cons of either of these really.

Any pearls of wisdom please on the best approach… maybe there’s another way?

Thanks,
Tim.

Idea 1

        votingDb:  [
          {
            username: string
            email: string
            password: hash? with auth plugin?
            polls: [
              {
                username: string
                pollName: string
                pollOptions: [{'batman': 5}, {'He-Man': 0}]
              }
            ]
          }
        ]

Idea 2

        users:  [
          {
            username: string
            email: string
            password: hash? with auth plugin?
          }
        ]

        polls: [
              {
                username: string
                pollName: string
                pollOptions: [{'batman': 5}, {'He-Man': 0}]
              }
            ]

Go with Idea 2 … decouple users and polls, they are different things so should have different collections

Thanks Sergio, very speedy response :slight_smile:

MongoDB’s document model is optimized for how the application accesses data (as developer time and speed to market are now more expensive than storage).

You should think about what data a given view will need and provide all the data in a single document.

Thanks. I think your comment complements what Sergio said. Idea 2 has the info I need for the view in one collection.

Cheers,
Tim.

1 Like

Database info should always be specific and separated. If needed be one, use a different table and join the two info together.