Firestore的两个字段顺序 [英] Firestore order by two fields

查看:87
本文介绍了Firestore的两个字段顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在firestore中,我想知道是否有一种方法来获取hueristic1并获取两个hueristic1值之间的所有数据,但根据hueristic2排序结果.

In firestore I'm wondering if there is a way to have a hueristic1 and get all data between two hueristic1 values but order the results based on a hueristic2.

我问是因为两页底部的数据

I ask because the data at bottom of both pages

https://firebase.google.com/docs/firestore/query-data/order-limit-data

https://firebase.google.com/docs/firestore/query-data/query-cursors

似乎有些矛盾的文件.

我想做的是

Ref.startAt(some h1 value).endAt(some second h1 value).orderBy(h2).  

我知道我可能必须按h1进行索引,但是即使那样我也不确定是否有办法做到这一点.

I know I'd probably have to index by h1 but even then I'm not sure if there is a way to do this.

推荐答案

更新:

我没有对此进行足够好的测试,以至于无法产生所需的排序. OP 再次提出了问题,并从Firebase团队成员那里得到了答案:

I didn't test this well enough to see that is doesn't produce the desired ordering. The OP asked the question again and got an answer from a Firebase team member:

因为Cloud Firestore不支持按其他字段排序 而不是所提供的不等式,您将无法按名称排序 直接从查询中.相反,您需要对客户端进行一次排序 您已经获取了数据.

Because Cloud Firestore doesn't support ordering by a different field than the supplied inequality, you won't be able to sort by name directly from the query. Instead you'd need to sort client-side once you've fetched the data.


该API支持所需的功能,尽管在显示它的文档中没有看到示例.


The API supports the capability you want, although I don't see an example in the documentation that shows it.

查询词的顺序很重要.假设您有一个城市集合,并且感兴趣的字段是population(h1)和name(h2).要获得人口在1000至2000之间的城市(按名称排序),查询将为:

The ordering of the query terms is important. Suppose you have a collection of cities and the fields of interest are population (h1) and name (h2). To get the cities with population in range 1000 to 2000, ordered by name, the query would be:

citiesRef.orderBy("population").orderBy("name").startAt(1000).endAt(2000)

此查询需要一个复合索引,您可以在控制台中手动创建.或如文档所示,系统将为您提供帮助:

This query requires a composite index, which you can create manually in the console. Or as the documentation there indicates, the system will help you:

您可以在您的查询中运行查询,而不是手动定义复合索引 应用程序代码以获取用于生成所需索引的链接.

Instead of defining a composite index manually, run your query in your app code to get a link for generating the required index.

这篇关于Firestore的两个字段顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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