Firestore 查询默认顺序

Firestore Query Default Order

以下是官方documentation中的表述:

By default, a query retrieves all documents that satisfy the query in ascending order by document ID. You can specify the sort order for your data using orderBy(), and you can limit the number of documents retrieved using the limit().

但是我发现上面的说法是错误的。我已将文档 ID 存储为递增数字 1,2,3...

但是当我通过以下代码获取文档 ID 时。

firebaseFirestore.collection(documentPathOnFireStore).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                for(DocumentSnapshot documentSnapshot : task.getResult().getDocuments()){
                    documentsIds += documentSnapshot.getId() + ","
                }
         }});

我收到了以下结果

1, 10, 100, 101, 102....

真的有什么方法可以得到文档 ID 升序排列的结果吗?

I received the following results:

1, 10, 100, 101, 102

这是预期的行为,因为您很可能将这些值存储为字符串,而不是存储为数字。请记住,在对字符串进行排序时,顺序为 lexicographical。例如,10 放在 2 之前。请看下面的示例:

1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3

Is there really any way to get the result in ascending order of document Ids?

是的,您唯一的选择是将字段类型更改为数字而不是 字符串。或者,从以下 post:

查看我的回答