问题描述
让我们假设上面的Firebase数据库架构.
Let's suppose above firebase database schema.
我要检索的消息是在"15039996197"时间戳之后的消息.每个消息对象都有一个createdAt
属性.
What I want to is retrieve messages which after "15039996197" timestamp. Each of message object has a createdAt
property.
如何仅在特定时间戳记后才能收到消息?在这种情况下,最后两条消息就是我要检索的消息.
How can I get messages only after specific timestamp? In this case, last two messages are what I want to retrieve.
我尝试了firebaseb.database().ref(`/rooms/$roomKey/messages`).startAt('15039996197')
,但是失败了.返回0.我该怎么办?
I tried firebaseb.database().ref(`/rooms/$roomKey/messages`).startAt('15039996197')
but failed. It return 0. How can I do this?
推荐答案
查询的情况是,期望message
节点应以数字开头,在这种情况下,您需要一个子节点名称createdAt
.因此,在这种情况下,您必须指定要按createdAt
进行订购,因此您需要这样做
The case with your query is that it's expecting that the message
node should have a number value to start with, in that case you want a child node with the name createdAt
. So in that case you must specify that you want to order by createdAt
, thus you need to do this
firebase.database().ref(`/rooms/$roomKey/messages`).orderByChild('createdAt').startAt('15039996197').on(//code);
这样,它将返回message
内具有名为createdAt
的子节点的所有节点,并从15039996197
开始.对查询进行排序可能会降低性能,因为我很高兴看看.indexOn规则.
This way it'll return all nodes inside message
that has a child named createdAt
an it starts at 15039996197
. Ordering your query may be a little bad for performance, for that i sugest taking a look at .indexOn rule.
有关更多信息,请此处.
希望这会有所帮助.
这篇关于如何在Firebase查询中使用startAt()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!