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

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

问题描述

一周前我问了一个问题.Firestore 按两个字段排序

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

我得到的回应是:

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