排序Firestore文件 [英] Sorting Firestore Documents
问题描述
有什么可能的方法可以根据特定字段值上的数字对集合的Cloud Firestore文档进行排序.例如,在 ids
集合中有文档,在每个文档中都有一个名为 idNumber
的字段.该idNumber的值为0,然后在下一个文档中为1,然后为2,以此类推.
Is there any possible way I can order the Cloud Firestore documents of a collection based on a number at a value of a specific field. For example in the collection ids
there are documents and in each one there is a field called idNumber
. The value of that idNumber is 0 then in the next document is 1 then 2 etc. Is it possible?
提前谢谢!
推荐答案
有可能吗?
是的!要通过特定的数字属性对集合中的文档进行排序,请使用以下查询:
Yes it is! To order the documents within a collection by a specific numeric property, please use the following query:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference idsRef = rootRef.collection("ids");
Query query = idsRef.orderBy("idNumber", Query.Direction.ASCENDING);
此查询将根据 idNumber
属性的升序对文档进行排序.如果您想要相反的顺序,只需将方向更改为 DESCENDING
.就是这样!
This query will order your documents according to the idNumber
property ascending. If you want a reverse order just change the direction to DESCENDING
. That's it!
According to the official documentation of Query's orderBy(String field) method:
创建并返回一个新的查询,该查询还会按指定的字段进行排序.
Creates and returns a new Query that's additionally sorted by the specified field.
And according to Query's orderBy(String field, Query.Direction direction) method:
创建并返回一个新的查询,该查询还会根据指定的字段进行排序,可以选择以降序而不是升序进行排序.
Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.
这篇关于排序Firestore文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!