Reputation: 81
I am using cloud firestore in my android chat application, I want to store the all messages (incoming and outgoing, basically maintaining message history for a device) in array-list of a document, but the maximum size of a document is 1MB. What would happen if the size of document exceeds 1MB when I am trying to store the message
Is there any API is available to get the size of the document before writing?
Please suggest me better way to maintain message history using firestore?
Upvotes: 8
Views: 4677
Reputation: 138814
Edit: Apr 13th 2020:
To always stay below the maximum of 1 MiB (1,048,576 bytes) limit, there is a library that can help you check that:
The algorithm behind this library is the one that is explained in the official documentation regarding the Storage Size.
What would happen if the size of the document exceeds 1MB when I am trying to store the message
The size of the document cannot be exceeded. You are limited at 1MiB.
Is there any API is available to get the size of the document before writing?
As far as I know, there is no API for doing that.
please suggest a better way to maintain message history using Firestore?
Store all the history messages as documents in a collection. If you are interested, here you can find a tutorial on how to structure a Firestore database for a chat application.
Upvotes: 3
Reputation: 61
If the document size exceeds 1MB. It will throw an error like this "Error: 3 INVALID_ARGUMENT: A document cannot be written because it exceeds the maximum size allowed."
Me too think that there is no API for getting the size of the document. One way is that, You can get the entire data from collection and stringify that, remove the white spaces and count the number of chars then add a buffer value based on the documentation https://firebase.google.com/docs/firestore/storage-size
Upvotes: 6