How to get documents descending by DocumentID?

醉酒当歌 提交于 2020-08-07 01:28:30

问题


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"

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/52119208/how-to-get-documents-descending-by-documentid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!