【问题标题】:(React-firebase-hooks) How to query firebase documents in descending order(React-firebase-hooks) 如何按降序查询firebase文档
【发布时间】:2021-10-20 11:25:40
【问题描述】:

我相信 firebase cloud firestore 按升序查询文档。我在网上看到了一些建议将值作为负数启动的解决方案,但它是一个时间戳对象,对应于 firebase 中的数据字段,因此将其存储为负数肯定行不通,对吧?当然,我可以有另一个已排序的字段并将其用作 .orderBy 参数,但最好避免这种情况。

我正在构建的这个应用程序是一个聊天应用程序,因此需要按时间戳降序查询消息。这是我目前检索数据的方式。

    const messagesRef = firebase.firestore().collection(props.chatNum);
    const query = messagesRef.orderBy('timeSent').limit(10);
    const [messages] = useCollectionData(query, { idField : 'id' });

    return (
        <div className="messageBox">
            {messages && messages.map(message => (
                <Message key={message.id} text={message.text} spent={message.timeSent}/>
            ))}
        </div>

    )

任何帮助将不胜感激!!!

【问题讨论】:

    标签: javascript reactjs firebase google-cloud-firestore


    【解决方案1】:

    Google Firebase 拥有这个几乎是秘密的资源,称为“文档”,您可以使用它来了解它的各种选项,例如:

    https://firebase.google.com/docs/reference/js/firebase.firestore.Query#orderby

    orderBy
    orderBy ( fieldPath :  string | FieldPath ,  directionStr ? :  OrderByDirection ) : Query < T >
    Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.
    
    Parameters
    fieldPath: string | FieldPath
    The field to sort by.
    
    Optional directionStr: OrderByDirection
    Optional direction to sort by (asc or desc). If not specified, order will be ascending.
    
    Returns Query<T>
    The created Query.
    

    【讨论】:

    • LMAO,对不起,我真的到处找这个,但就是找不到。所有这些帖子都指的是具有不同值的插入。谢谢!
    猜你喜欢
    • 2022-06-20
    • 2018-07-06
    • 2022-12-05
    • 1970-01-01
    • 2021-12-12
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    相关资源
    最近更新 更多