5 ms·
I have a Node server that restarts once a day. On start it reads the state of the app (which is just one big JSON) from MongoDB into a JavaScript variable. When
by Kiro 9y ago
I have a Node server that restarts once a day. On start it reads the state of the app (which is just one big JSON) from MongoDB into a JavaScript variable. When something changes it updates the variable in the Node process and fires an event to update the field in MongoDB. In other word it only ever reads from MongoDB on server start. This has worked very good so far but with all the negative comments I'm thinking there must be some kind of drawback or reason I should switch to Postgre JSON instead.
One problem I see is that I need to overwrite the whole thing when saving with Postgre when you can easily just set one field in MongoDB. If I need to overwrite I may as well just save the whole state to a file instead.
Forgot to add that it does the same for users. Users are one collection which reads the document on login and saves the same way as the global state does on every update. How would that work with Postgre?
- texuf 9y agoMongo is great for certain use cases. And if it ain't broke don't fix it.
- 6nf 9y ago> Mongo is great for certain use cases. People keep saying this but I haven't seen such a case yet.
- icebraining 9y agoI'm not saying you should switch to PG, but since version 9.5 it does allow you to update JSON keys: https://stackoverflow.com/a/35349699/221786 https://stackoverflow.com/a/35349699/221786 Full docs: https://www.postgresql.org/docs/9.5/static/functions-json.html https://www.postgresql.org/docs/9.5/static/functions-json.ht...
- jerf 9y agoWhile I have no particular stake in whether you rewrite, presumably in the Postgres rewrite, you'd break the monolithic JSON file up to be one row per key you write. But that on its own would hardly be a reason to change. "Forgot to add that it does the same for users. Users are one collection which reads the document on login and saves the same way as the global state does on every update. How would that work with Postgre?" Well, that boils down to "how do relational databases work?", which is not really a suitable HN comment topic. Relational databases are designed fundamentally differently. It doesn't matter as much as long as you stay under a scaling limit where reading and writing the entire state all the time isn't a problem for you, and there's no need to do any sort of query like "Tell me the last login time for all users in a fraction of a second, please". What is probably the best thing for you to do is A: budget some time to read about and maybe even do some playing with relational databases so that B: when you encounter a problem for which they are the best solution, you realize that you've done so, and can take appropriate action. "Appropriate action" doesn't always mean "rewrite the entire website in Postgres"; sometimes it can be "denormalize the data into a Postgres db, run the queries I need, and throw the database away", which can, in some circumstances, still be faster than trying to convince a NoSQL to do queries it really doesn't want to do. Especially on a production database that's busy trying to do the real work. The idea here is to make sure you don't get sucked into the tarpit of accidentally trying to replicate relational database features at your application level, which starts so beguilingly small and grows to consume your entire development budget if you start down that road accidentally and don't turn away soon enough, rather than to do things the "right" way for the abstract sake of doing it right. Oh, and here's a tip that can save your job and/or career: If you ever find yourself in a position where you're trying to implement "transactions" in your application code, immediately stop and go get an appropriate database on the backend. The horrifying thing about this case isn't that you'll fail... it's that you will totally produce a solution that works perfectly in QA, and fails miserably with data destruction and angry (if not actively litigious) customers in production, and the road of "temporary fixes that seem to fix the problem but actually make it worse" is basically never ending. Learn what this looks like so you know it when you see it!
- Kiro 9y agoThank you for your very detailed and good response. I actually have a lot of experience with relational databases, much more than with NoSQL. The thing with the users collection is that I need to be able to add any arbitrary attribute to the user document so I can't really use a table with a schema. I guess I could use a related key value table for those but the attributes can be anything, nested objects and arrays. Maybe serializing the value would work but it still seem that Mongo is a better fit in that case. I also like seeing the user document as a big JSON without joins when doing DB admin stuff.
- perlgeek 9y ago> The thing with the users collection is that I need to be able to add any arbitrary attribute to the user document so I can't really use a table with a schema. You could put the attributes that all users share (username, email address, maybe hashed password) in a table, and then have a jsonb or hstore field for the rest of the attributes. That way you can still programmatically access the elements, and even build indexes over them. But if you have no real reason to change DB, don't.
- Kiro 9y agoThat's a good idea. Thanks.
- 6nf 9y ago> I need to be able to add any arbitrary attribute to the user document so I can't really use a table with a schema. I'd love to see a sample of this data
- deleted 9y ago[deleted]
- Kiro 9y agoIt's not your usual CRUD app. It's a realtime sandbox game in early access. Some arbitrary stuff I keep adding for various reasons: gunsLeftToCompensateDueToChangeInVersionX: 123, winnerOfEventX: true, didSomethingFunky: Date.now(), noOfEnemiesKilledBeforeVersionY: 123, inventory: [{id: GENERIC_ITEM_ID, quantity: 123}, {id: uuid, name: "Ultimate Weapon", damage: 10000, someUniqueProp: {doThis: true, uses: 123}}], flaggedForCheatingInThisOrThatArena: true Etc etc. Biggest reason though is just convenience of adding some state to the player when developing new features. Itt would be a complete pain having to add schemas for every single situation, even if it's standardised for all players. Now I just add it to the player object and it's saved automatically to the player document. Not saying what I do is best practice or anything but it enables me to move fast without having to do much DB admin stuff.