Retrieve a random 15 children from Firebase

2019-09-07 02:32发布

问题:

I have a Firebase database with Items in it. There could potentially be up to 1000 items in the database.

I am looking to pull 45 random children out of the database to use.

Any idea how I can do this without pulling them all out first and then weeding them down to what I need?

回答1:

Assign each item an index, 1-1000

-Jhsu498984
  item_name: "my item 0"
  item_index: 0
-Ynkkj93ov9
  item_name: "my item 24"
  item_index: 24

then, with a random number generator, generate 45 random numbers (which match the item_index) and query for those specific items.

or

create all of the items and in separate node, keep their node refs

item_refs
  -Jhsu498984: true
  -Ynkkj93ov9: true

then you just need to load in the item_refs (into an array) and then randomly pick 45 from the array. Then query for those items.