简体   繁体   English

从 firebase firestore 检索后,如何根据字段对 firestore 集合文档进行排序

[英]how to sort firestore collection documents based on a field after retrieving from firebase firestore

Assume that I have a Firestore collection with the name users .假设我有一个名为users的 Firestore 集合。

Also assume that each document of the collection users have the following fields in each document:还假设集合users的每个文档在每个文档中都有以下字段:

name
surname
year

When fetching the data from Firebase Firestore, the following code can be run:从 Firebase Firestore 取数据时,可以运行如下代码:

Query myData = FirebaseFirestore.instance
          .collection('users')
          .orderBy('name');

And this code piece sorts the documents based on the name field in each document when retrieving it.这段代码在检索文档时根据每个文档中的name字段对文档进行排序。

Keeping in mind that you are charged based on the number of reads when fetching data from Firestore, I wonder how I can re-sort the retrieved data from Firestore without increasing the number of read operations for my account ?请记住,您是根据从 Firestore 获取数据时的读取次数付费的,我想知道如何在不增加我帐户的读取操作次数的情况下对从 Firestore 检索到的数据进行重新排序?

For instance, the above code piece sorts the documents based on the name field.例如,上面的代码片段根据name字段对文档进行排序。 How can I sort the documents based on surname or year fields after that?之后如何根据surnameyear字段对文档进行排序?

Is running the same piece of code by changing orderBy('name') with -eg- orderBy('surname') an option?是否通过使用 -eg- orderBy('surname')选项更改orderBy('name')来运行同一段代码? Or does it increase my number of reads?或者它会增加我的阅读次数吗?

If you perform a second query with a different orderBy field, you will incur the cost of reading all the documents again.如果您使用不同的orderBy字段执行第二次查询,您将承担再次读取所有文档的成本。

It would be far more efficient in terms of cost and speed to write code to sort the documents again after you have them in memory from the first query.在您从第一个查询中获得 memory 中的文档后,编写代码再次对文档进行排序在成本和速度方面会更有效率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM