【问题标题】:How to order documents by its auto-generated IDs ? (Cloud Firestore)如何通过其自动生成的 ID 对文档进行排序? (云防火墙)
【发布时间】:2020-04-19 14:59:00
【问题描述】:

我一直在尝试找出如何按 ID 以升序或降序(没关系)对文档进行排序,但找不到一个单一的解决方案。我找到了按时间戳排序,但不想这样做,因为可以同时创建多个文档。

我需要通过自动生成的 id 对文档进行排序,因为我正在实现分页:

retrieveMoreRestaurants = async () => {
        this.setState({
            refreshing: true,
        });
        var additionalQuery = await firebase.firestore().collection('restaurants')
            .orderBy('__name__')
            .startAfter(this.state.lastVisibleDiscount)
            .limit(this.state.limit)

        var documentSnapshots = await additionalQuery.get();
        var All = documentSnapshots.docs.map(document => document.data());
        var lastVisible = All[All.length - 1].res_id;
        this.setState({
            All: [...this.state.All, ...All],
            lastVisible: lastVisible,
            refreshing: false,
        });
};

【问题讨论】:

    标签: react-native pagination google-cloud-firestore


    【解决方案1】:

    正如official documentation中所述:

    默认情况下,查询会按文档 ID 升序检索满足查询的所有文档。

    基本上,如果您需要它们按 ID 升序排列,您应该只实现默认查询。

    您还可以查看其他StackOverflow post 中有关此主题的大型讨论。

    【讨论】:

    • 我这样做是因为我需要知道最后获取的项目: var additionalQuery = await firebase.firestore().collection('restaurants') .orderBy(firebase.firestore.FieldPath.documentId() ) .startAfter(this.state.lastVisible) .limit(this.state.limit)
    猜你喜欢
    • 1970-01-01
    • 2013-03-09
    • 2019-09-23
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 2019-08-21
    • 2016-02-01
    • 1970-01-01
    相关资源
    最近更新 更多