I am encountering the issue encountered in this question. Because the other question (and answer) is missing the point of solving the problem, I am writing a new question.
orderBy(FieldPath.documentId(), 'desc')
The above query will fail with the following error message:
The query requires an index. You can create it here: ...
As described in the other question, it is impossible to create that index because of the following error message:
__name__ only indexes are not supported
Because the opposite query works fine:
orderBy(FieldPath.documentId(), 'asc')
I am wondering why the descending query does not work. To me that does not really make a lot of sense, especially because this should be a very common use case.
It would be awesome if there is a solution to the above problem, i.e. if there is a way to create an index that makes 'desc'
work. However, I expect that it just is not a possibility and will therefore phrase a broader problem.
How do I really get my documents in descending order by the Document ID?
I could obviously do the 'asc'
query and then reverse my retrieved list.
But this does not work because I need to limit()
the query. I cannot just pay for all the documents in a collection just to get a descending order.
This way I would get billed for all document reads in the collection even if I just wanted to get a single one (the last one). Here is a great article discussing potential consequences of this because it does not scale.
Do I really need to create a field called id
, which contains the exact same documentID
, just to create an index on it and then be able to query descendingly?
A potential collection could look like this:
"q":
- id: "q"
"u":
- id: "u"
"i":
- id: "i"
"t":
- id: "t"
"e":
- id: "e"
"d":
- id: "d"
"u":
- id: "u"
"m":
- id: "m"
"b":
- id: "b"