Reputation: 30879
From the documentation Firestore has, among others, these limitations:
Maximum number of index entries for each document: 40,000
Maximum sum of the sizes of a document's index entries: 8 MiB
I'm having some difficulty understanding what the docs mean by a "document" here. Do they refer to one single document, or the whole set of documents of a specific collection?
For example, suppose I have a collection which contains 10,000 documents, and each document contains the same 2 fields:
Document: `students/00001`
name: "Jeff"
age: 32
Document: `students/00002`
name: "Mary"
age: 28
...
Document: `students/10000`
name: "Anne"
age: 45
According to the docs, Firestore will create one index for each field in ascending order, and one more index for each field in descending order. Thus, we'll have 4 indexes for each document, and only these same 4 indexes for the entire collection of documents, since they all have the same fields.
All is well.
However, now suppose I have a collection which also contains 10,000 documents, but where each document contains 2 fields which are always different from each other:
Document: `students/00001`
B2DcG6g9w: true
YwY3g642D: true
Document: `students/00002`
A3h7k3e6B: true
w2Fh0j1g8: true
...
Document: `students/10000`
8n4545f7k: true
j2hle4oOp: true
Firestore will create one index for each field in ascending order, and one more index for each field in descending order. Thus, we'll have only 4 indexes for each document. But since these indexes are different for each document we will have a total of 40,000 indexes for all the documents in the collection.
Question: Have I reached the 40,000 index limit, or not? Is that limitation really by document or is it by collection?
Upvotes: 1
Views: 658
Reputation: 598718
There's a single index created for each field or combination of fields that you index in a collection. Each document can be present in 40.000 of such indexes.
Upvotes: 1
Reputation: 317362
The number of documents in the collection is irrelevant to the stated limits. Documents that lack a field won't have an index for that field. (Firestore won't index on something that doesn't exist.) You are able to remove indexes in order to save space and money.
Upvotes: 2