简体   繁体   中英

How to get random keys in Firebase Database without fetching all data from a reference node?

I'm trying to get random keys from my Firebase Database which is a set of questions but all the solutions I found so far suggested fetching all the data first then getting the size and selecting random keys from that data. I want to know if it's possible to do that without having to fetch all data considering that the set could get larger.

I thought of saving the keys to another node and fetching that node only and then using those selected keys, I can get only the questions I need thus reducing unnecessary data but would that even make sense?

If you want to get a subset of the data from a large node into your application, and you cannot achieve that means by using a query to slice the data, it is quite common to duplicate the subset of the data into its own separate node.

An example (since you unfortunately remain rather generic in your question): if you have a list of user profiles (say /users ), but are only trying to show a list of user names in your application, it is wasteful to retrieve the full user profiles. In such cases, it is common to create a node with just the user names and the UIDs (say /usernames ).

You'll typically either create the derived list from a trusted environment (such as your development machine, a server you control, or Cloud Functions) or use security rules to ensure the data in the derived list remains in sync with the main list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM