Firestore allows scaling your product indefinitely, as long as you consider its limitations. One of such limitations is the number of index entries per document, which is at the moment is 40,000.
If you see too many index entries for entity
in Firebase
Functions logs, a web or mobile app, it means that one of your documents
hit the limit.
Firestore automatically adds single-field indexes to all your fields, including nested maps and list items. So if you have a document of the given shape:
{
"name": "Laughing Potion",
"ingredients": [
"Spring water",
"Alihotsy leaves",
"Billywig wings",
"Knarl quills",
"Puffskein and scatter hair",
"Horseradish powder"
],
}
Even without adding any composite indexes, the document would have 7 index
entries, one for name
and one for each item in
ingredients
list.
If you would add a composite index name ASC, ingredients ASC
,
it will increase the number of the entries two times.
Still far from 40,000, hah?
It's actually not trivial. That's why you probably never heard about this limit before. I managed to hit it when I was storing analytics data in a similar shape:
{
"tweets": {
"04AHqbIwFB39P6y29Xjv": {
"0oKZLetIxqD6TqV5BAOO": {
"likes": 28,
"retweets": 9
},
"0Zxz3UtZ1Z3dUp997hxW": {
"likes": 2,
"retweets": 0
}
}
},
"threads": {
"04AHqbIwFB39P6y29Xjv": {
"likes": 30,
"retweets": 9
}
}
}
The number of nested fields would increase with each tweet by at least four. After a few months of running the app in production, I started seeing this error as these documents accumulated a lot of data.
too many index entries for entity
?The solution is actually easy — add an exemption. To do that, head to Firestore > Indexes > Single field and find "Add exemption" button at the bottom of the page:
Then enter the collection ID, field path to exclude (in my case that would
be tweets
and threads
, an exemption for each),
and the query scope. Press "Next" and wait for indexation to complete:
And that would be it!