I’m little confused that if I wanna store json objects and arrays in db than which way is better. I can either directly store them as they are or I can first convert it to string with method
JSON.stringify() and then store it. And reuse it with method
I’m using cloud firestore as my db and as it has 20K field limits on one document, one thing I concluded is that whichever object has a possibility of having more than 20K fields (e.g. canvas drawings), those should be stored as a string. But still I want to have some more clearity about which method should I prefer for small arrays and objects which would be:
- Cost effective
- Optimised in performance
- Other factors which I don’t know
Firestore is specifically optimized and marketed for storing JSON objects, which means that if you don’t use this format, you are going to miss out on a lot of functionality provided by Firestore and would have to implement it yourself.
For example, storing the user’s data as a string (in a JSON format, but without telling Firestore that it is) would mean that you wouldn’t be able to use the powerful firestore query system, because it won’t know which format the data is in, since it’s all just a string. Therefore, you would have to fetch all users to the client, and then do queries on that data manually, which means taking up a lot of RAM and network.
When updating, you would have to transfer all bytes of the object’s string representation through the network in order to modify even a single small field. If stored as an object, however, you can modify specific fields without the need of transfering the whole payload.