I am currently using the snapshot.forEach method to maintain order (which is essential in my application), but this requires me to do a snapshot deep query. Is there a way to do the same functionality without running a full deep query?
CodePudding user response:
The only API surface that can get a key without its value from the Realtime Database is its REST API: https://firebase.google.com/docs/database/rest/retrieve-data#shallow
The only (and default) ordering that happens there is by the node keys. Depending on the tool you use the read the JSON they may show up unordered there though, so you should always re-order them explicitly in your code, but you can use the key order to get a slice of the data with startAfter
and limitToFirst
.
An example, using this data from another question a while ago: https://stackoverflow.firebaseio.com/70102492/Main/News/Categories/Education.json?print=pretty
You can get the first three node keys with: https://stackoverflow.firebaseio.com/70102492/Main/News/Categories/Education.json?print=pretty&shallow=true&limitToFirst=3
And then the next three with: https://stackoverflow.firebaseio.com/70102492/Main/News/Categories/Education.json?print=pretty&shallow=true&limitToFirst=3&startAfter=-Mn7-ZxkUPO01iddfh2tpEn