Most appropriate Mongo database schema layout? Voting App

Most appropriate Mongo database schema layout? Voting App
0

#1

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}]
              }
            ]

#2

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


#3

Thanks Sergio, very speedy response :slight_smile:


#4

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.

https://www.mongodb.com/blog/post/thinking-documents-part-2


#5

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

Cheers,
Tim.


#6

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