One or two collections?

I’m creating an app for regular players of laser tag to check-in at the front desk.

My question is should I normalize the data and use two collections or one players collection with embedded check-ins.

I’d like to pull reports at some point of how many times a specific player checked in.

I know I could technically do this either way but was looking for some advice from you guys on how you would do it using meteor and mongo. Coming from MySQL everything seems backwards to me.

It depends on so many things and in the end you’ll probably need to make the decision yourself, but in this case I’d advise on making a separate collection for checkins, some reasons I can immediately come up with:

  • Storing checkins into the user collection would mean every time a user logs in this data is automatically downloaded as well, might become inefficient if they have 1000s of logins (not very probable, but you know).
  • Separating the data allows for easier querying. For example, running a query that gets all the checkins in a certain day will run a lot faster and more efficient if you have a checkins collection since you won’t need to iterate over all users’ objects.

Ultimately a checkins object only needs some minimal data like a timestamp and the userId, but could easily be extended in the future as well.

Yeah, I have it with two collections currently using the time stamp and playerId. Only because that’s what makes sense in my head from SQL. But I read so much about denormalization it’s got me all out of whack. Lol. All of what you said makes sense. Thank you.

Yo normalize till you can’t anymore. Sure querying will be a pain in the ass but that’s what helper functions are for.

I’d say that if you need to keep the timestamp for all occurrences, then definitely a separate collection for check-ins. Besides the reasons @nlammertyn mentioned (which are super important in the Meteor context due to how data flows), if you embed check-ins in a player document, mongo will need to move it on disk as the document grows, which is inefficient. If there’s some sort of frequent info you’ll need to query with the player document, then it may be worth denormalizing it.

Moreover, since you want to generate reports at some point, depending on how complex they are you’ll probably want to index this somehow. Reasoning about this in a separate collection should be easier. And you might even need to pre-aggregate this data in a separate stats collection.

Anyway, at the end it all depends on your use cases!

1 Like

Thanks guys for the help. Got it figured out. Using two collections.