Firestore 按两个字段排序 [英] Firestore order by two fields

查看:34
本文介绍了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/查询数据/查询光标

文档似乎有些矛盾.

我想做的是

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天全站免登陆