How to use startAt() in Firebase query?

Firebase Data Retrieval works node by node. So whatever data you want to get, the entire node is traversed. So in your case to get any message your complexity would be O(number of messages).

You would want to restructure the way you are storing the data and put createdAt in Node instead of Child.


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);

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.

For more information take a look here.

Hope this helps.