问题
Problem:
I'm building an app where I want to display a list of 50k locations. Knowing that the size of geographical point is 16 bytes, I decided to store all of them in a single document.
The location where I store this list is:
db.collection("points").document("geo");
I know that the size of a document is composed of three components:
- document name size
- The sum of the string size of each field name
- The sum of the size of each field value
- 32 additional bytes
So in my case, I have:
- (6 + 1) + (3 + 1) + 16 = 27
- 9 + 1 = 10 as the name of the field is
geoPoints- 50,000 * 16 = 800,000
- 32 additional bytes
So in my case the total is 800,069 bytes which less than 1,048,576 bytes as it is mentioned in the docs. If I try to add this list to the above document, I get:
INVALID_ARGUMENT: A document cannot be written because it exceeds the maximum size allowed.
However, I'm not able to add less than that. I made some tests and I could only write a number of 40,327 GeoPoints, meaning:
- (6 + 1) + (3 + 1) + 16 = 27
- 9 + 1 = 10 as the name of the field is
geoPoints- 40,327 * 16 = 645,232
- 32 additional bytes
So a total of 27 + 10 + 645,232 + 32 = 645,301 bytes, which is much less than what says in the docs.
If I try to write 40,328 instead of 40,327, I get the same error.
Question:
How to store the total of 1,048,576 bytes, as it is mentioned in the docs?
回答1:
The document also stores a data type for each field. You can see this in the JSON that is returned from the http api. My guess would be that these data type tags also count against the size limit.
Depending on the access rules for your database you can also see this by accessing a document directly via its url:
https://firestore.googleapis.com/v1/projects/PROJECT_NAME/databases/(default)/documents/COLLECTION_NAME/DOCUMENT_ID
{
"name": "projects/PROJECT_NAME/databases/(default)/documents/COLLECTION_NAME/DOCUMENT_ID",
"fields": {
"Name": {
"stringValue": "Hobbies"
},
"Active": {
"booleanValue": true
}
},
"createTime": "2019-12-02T16:03:07.111185Z",
"updateTime": "2020-03-12T13:44:01.827176Z"
}
来源:https://stackoverflow.com/questions/61103830/firestore-document-does-not-have-1-mib-1-048-576-bytes