16

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"
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402

2 Answers2

5

Currently it's not possible to perform a descending query based on document ID. Your alternative is to put the document ID in a field of the document, and use that for ordering.

Feel free to also file a feature request for this, but it's not likely to happen in the near term.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    Since this answer you guys have introduced the .limitToLast() method for queries. It's possible to do startAfter(lastDocSnap.id).limit(n) but endBefore(firstDocSnap.id).limitToLast(n) results in the __name__ only indexes are not supported. This implies there is a implicit ascending index behind the scenes but not a descending one by default? Why not both? – Andy Fusniak Oct 01 '20 at 12:26
  • This answer is outdated, as you can do this now. – Jonathan Aug 28 '21 at 04:53
0

This is a weird work-around, but it is possible to sort on document ID descending if you have additional where conditions. This is PHP. I have only tested this with equality conditions.

So, even though this doesn't work (failing with a create index link that leads to the error "__name__ only indexes are not supported"):

$docs = $fsClient
    ->collection('incidents')
    ->orderBy(FieldPath::documentId(), 'DESC')
    ->documents();

This does:

$docs = $fsClient
    ->collection('incidents')
    ->orderBy(FieldPath::documentId(), 'DESC')
    ->where('dummy-field', '=', true)
    ->documents();

This does require you to add a dummy-field with a value of true to all your documents.

9072997
  • 849
  • 7
  • 21
  • EDIT: This does not appear to actually sort in descending order – forresthopkinsa Aug 09 '21 at 07:12
  • @forresthopkinsa I just double-checked, and this does work for me. If you have time, I would be interested to see an example where this technique does not result in results being sorted in descending order. – 9072997 Aug 09 '21 at 14:55
  • Are you using automatic or manual document IDs? – forresthopkinsa Aug 10 '21 at 01:51
  • My use case was that the document IDs were numbers (as strings, of course), so: "1001", "1002", "1003"... and when I requested them in descending order using this technique, they returned in *ascending* order, which happens to also be insertion order – forresthopkinsa Aug 12 '21 at 08:16