0

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 Answer 1

4

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.

2
  • Thanks. I will think about this. It may be a little difficult to add the index numbers since the items are added on the fly by users. Nov 26, 2015 at 1:16
  • I can see where that would be an issue - Use the reference number then - it's auto-generated, discrete and created by childByAutoId or push() and could be used in the same fashion.
    – Jay
    Nov 26, 2015 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.