Marcelo Glasberg
Marcelo Glasberg

Reputation: 30879

Firestore index limitation

From the documentation Firestore has, among others, these limitations:

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

Answers (2)

Frank van Puffelen
Frank van Puffelen

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

Doug Stevenson
Doug Stevenson

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

Related Questions