Google cloud firestore Firestore查询Wheremore在一个字段上,OrderBy在不同字段上

Google cloud firestore Firestore查询Wheremore在一个字段上,OrderBy在不同字段上,google-cloud-firestore,Google Cloud Firestore,我想查询集合中位置字段条件满足的大于或等于或等于或等于的所有文档,然后按“人口”字段(即数字)的降序排列这些文档,并获得前10个人口位置 _firestore.collectionGroup(_collectionGroupName) .where("location",isGreaterThanOrEqualTo: lowGeoPoint) .where("location",isLessThanOrEqualTo

我想查询集合中位置字段条件满足的大于或等于或等于或等于的所有文档,然后按“人口”字段(即数字)的降序排列这些文档,并获得前10个人口位置

_firestore.collectionGroup(_collectionGroupName)
          .where("location",isGreaterThanOrEqualTo: lowGeoPoint)
          .where("location",isLessThanOrEqualTo: highGeoPoint)
          .orderBy("population", descending: true)
          .limit(10)
          .get();

Firestore给出了一个例外,我需要先添加.orderBy(“位置”),如果我这样做并为该查询添加复合索引,它只需按位置对文档进行排序,不需要按填充排序,但我不需要按位置排序。我只想按“人口”排序。这方面有什么解决办法吗?

这是Firestore的一个限制。不能按字段过滤,然后按其他字段过滤。它们必须是同一个领域。解决方案将添加另一个orderby条件,因为此限制仅适用于第一个orderby。像这样做:

_firestore.collectionGroup(_collectionGroupName)
          .where("location",isGreaterThanOrEqualTo: lowGeoPoint)
          .where("location",isLessThanOrEqualTo: highGeoPoint)
          .orderBy("location", descending: true)
          .orderBy("population", descending: true)
          .limit(10)
          .get();

请记住,您需要为此查询创建一个复合索引。

这是Firestore中的一个限制。不能按字段过滤,然后按其他字段过滤。它们必须是同一个领域。解决方案将添加另一个orderby条件,因为此限制仅适用于第一个orderby。像这样做:

_firestore.collectionGroup(_collectionGroupName)
          .where("location",isGreaterThanOrEqualTo: lowGeoPoint)
          .where("location",isLessThanOrEqualTo: highGeoPoint)
          .orderBy("location", descending: true)
          .orderBy("population", descending: true)
          .limit(10)
          .get();
请记住,您需要为此查询创建一个复合索引