在一个查询中按两个字段进行Firestore排序 [英] Firestore order by two fields in one query

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

问题描述

一个多星期前,我问了一个问题. 两个区域的Firestore订单

I asked a question a little over a week ago. Firestore order by two fields

我得到的答复是:

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

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

查询词的顺序很重要.假设您有一个城市集合,并且关注的领域是人口(h1)和名称(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:"

***但是返回的不是按名称排序的人口在1000到2000之间的城市,而是按名称排序的人口1000在城市,然后按名称排序的人口1001在城市,按名称排序的1002,依此类推到2000年.

***But what this returns is not cities with population between 1000 and 2000 ordered by name but rather cities with population 1000 ordered by name followed by cities with population 1001 ordered by name followed by 1002 ordered by name and so on up to 2000.

我想知道是否有办法按名称排序所有1000到2000年之间的城市. 谢谢.

I wondering if there is a way to get all cities between 1000 and 2000 in population ordered by name. Thanks.

推荐答案

我认为您正在寻找population上的where子句:

I think you're looking for where clauses on population:

citiesRef
  .where("population", ">=", 1000)
  .where("population","<",2000)

因为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.

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

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