1

I have this firebase data structure

enter image description here

I want to print all the keys under the firebase generated keys (i.e from 3030-00-809-7702 to newNSN) so I use this code

Inventory = db.child("Inventories").get()
for business in Inventory.each():
businessid = business.key()
productdb = db.child("Inventories").child(businessid).get()


for product in productdb.each():
    productid = product.key()
print(businessid)
print(productid)

but I what I get is this

enter image description here

so only the last keys are being printed instead of all the keys. What am I doing wrongly and how can I get it to print all the keys ?

KENdi
  • 7,576
  • 2
  • 16
  • 31
e.iluf
  • 1,389
  • 5
  • 27
  • 69
  • I notice that you keep using this Pyrebase library instead of the official one... Any reason why? https://firebase.google.com/docs/reference/admin/python/firebase_admin.db – OneCricketeer Sep 11 '17 at 12:13

1 Answers1

1

Just print the value at the current tree to get the whole thing

inventory = db.child("Inventories").get()
for business in inventory.each():
    print(business.val())

Or you go iterate it, which is really inefficient to request N items from Firebase for N children.

inventorydb = db.child("Inventories")
for businessid in inventorydb.shallow().get().each():
    productdb = inventory.child(businessid)
    # print the ids
    print([id for id in productdb.shallow().get()])
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • the reason why I wanted just the NSN numbers for each child is so that I can put all of them in an array. I want to collect about a thousand NSN numbers and put them in an array. – e.iluf Sep 11 '17 at 12:52
  • Okay, well, still. Heavily nested data isn't optimal in Firebase query model – OneCricketeer Sep 11 '17 at 12:54
  • Yea, I know that the deep nesting is a potential problem. that's why I was trying implement a different method and could not figure out how to do this https://stackoverflow.com/questions/46146691/how-can-a-new-data-be-added-to-firebase-without-the-post-keys – e.iluf Sep 11 '17 at 12:58
  • 1
    If you want to push a known name without overriding it, you'll need to create an incremental type of key. I don't have any examples of that though – OneCricketeer Sep 11 '17 at 13:13