Hi Guys! I have a front end background but I’m new to the databases and back end.
I like to be certain about the stuff that I work with and right now I need to structure my database and models in MongoDB.
I did read a lot of MongoDB articles about data modeling and different patterns, but I can’t properly fit my application into one of them.
Let’s say I have 4 collections:
User → Artist
Item → Product
I need to have a User collection with some user specific fields like name and avatar etc. Some Users can apply to become an Artist - Artist(a new collection) also has got some artist-specific fields like music genres and so on.
Artist always has to be a User first, but a User doesn’t have to be an Artist.
The other part of my app is about the Item and Product collections. An Item might have a title and description fields. An Item can become a Product and extends Item’s fields with Product ones like price for example.
For now we have a reference between a User and an Artist and Item and Product, but obviously there will be more references regarding the app itself. A User can buy a Product and an Artist can create an Item.
So that’s just a small part of the app - there will be more collections and references in the future.
I can’t find a way to model the data I have right now and I’m a bit worried about my current approach.
For now I see two possible solutions:
- I leave the collections as they are right now and whenever I want to get the Product or Artist data I populate Item and User accordingly.
- I merge User and Arist collection into one and leave the ‘artist’ fields empty as long as the User isn’t an artist. I do the same thing with Item and Product - I merge them into one collection and leave the ‘product’ related fields empty when creating a Item document and populate the fields if necessary.
My thoughts on both of those solutions:
First solution forces me to use populate(I use mongoose) or aggregate to join the collections whenever I need the data from both collections - Item & Product and User & Artist which results in having two queries. This might not be the best solution in terms of a performance, but I’m not sure if that will be the case.
Second solution feels weird for me - why should I have an empty fields on a merged collection when I don’t need them? I believe this approach will be more optimised cause I need to do just one query instead of two, but I have to say that the first solution feels more ‘natural’ for me.
Is any of those two solutions any good? Or should I find a different path? Should I be worried about joining the collections?
Thanks in advance, I really appreciate any help!