如何获取按DocumentID降序排列的文档? [英] How to get documents descending by DocumentID?

查看:45
本文介绍了如何获取按DocumentID降序排列的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到在此问题中遇到的问题.因为另一个问题(和答案)缺少解决问题的要点,所以我正在写一个新问题.

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__仅索引不支持

__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.

如果有解决上述问题的方法,那就好极了,也就是说,如果有一种方法可以创建使'desc'工作的索引.但是,我希望这是不可能的,因此会提出一个更广泛的问题.

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.

我显然可以执行'asc'查询,然后反转检索到的列表.
但这不起作用,因为我需要 limit()查询.我不能仅仅为了获得降序而支付集合中的所有文档的费用.
这样,即使我只想得到一个文件(最后一个),我将为所有文档读取收费一个).
这是一篇很棒的文章讨论此操作的潜在后果,因为它无法扩展.

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.

我真的需要创建一个名为 id 的字段,该字段包含完全相同 documentID ,只是要在其上创建索引并然后能够递减查询?
潜在的收藏可能看起来像这样:

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"

推荐答案

当前无法基于文档ID执行降序查询.您的替代方法是将文档ID放在文档的字段中,并用于订购.

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.

这篇关于如何获取按DocumentID降序排列的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆