I'm building a project and I only know the MERN stack. Everywhere I see the general consensus is that Mongo is not good and should only be used in very specific use cases. More importantly, I also want to expand my skills so I thought this would be a great time to try out PostgreSQL since there also seems to be a ton of jobs requiring this so I can kill two birds with one stone.
Here's my problem.
I'm building an application with unstructured data. What I mean is that I'm building something like a counter where users can input something and it will be counted how many times it has been inputted that day. To give an example lets say it's an app that tracks the snacks you've eaten throughout the day. In Mongo the structure would look like this
{ user_id: "someId", date: 1586822400, snacks: { "orange": 5, "kit-kat": 100, "peanuts": 30 } }
Then if the user were to again add "peanuts", it'll increment the value of peanuts
by 1 so it'll say 31
instead of 30
. However, if the user was to add a random snack called whizits
, it'll upsert a key-value pair so the db will look like this
{ user_id: "someId", date: 1586822400, snacks: { "orange": 5, "kit-kat": 100, "peanuts": 30, "whizits": 1 } }
Then once the day ends, a new document will be created with an empty snacks object and it'll start all over. This is unstructured because I have no idea what snacks the user will end up adding. Sure, there will probably be a few common snacks that will be added like fruits and chips and whatever else, but there will be the occasional whizits
.
How can I create a schema like this in Postgres? The only way I can think of this is to have a table with the snacks column just being a really long string of the snacks that are split by spaces or something so I can count all the snacks. So it would look like
+---------+------------+-------------------------------------------------+ | user_id | date | snacks | +---------+------------+-------------------------------------------------+ | someId | 1586822400 | "orange orange orange orange orange kit-kat ... | +---------+------------+-------------------------------------------------+
This just seems like a horrible way of doing this so any other ideas are appreciated!
What's important is I want writes to be very fast because I want to update in real time, like imagine if there were a ton of users entering snacks at the same time. I'm using web sockets to relay it as fast as possible. Reads won't happen as frequently since the only time a read will occur is when the user goes to their dashboard to see the statistics of how much snacks they ate and other data.
PS. Maybe this is one of those cases where Mongo or any other NoSQL db would do better? I posted my project elsewhere asking what db to use and people said to use a relational db since this is relational data since users have snacks, etc. Maybe it's because I'm new to this but I don't see how I can make this work. If you have any other suggestions I'm all ears!