0

If i have the following Firebase structure:

root:
    id1:
        subId1:
           prop: False
        subId2:
           prop: True
    id2:
        subId1:

    id3:
        subId1:
           prop: False
        subId2:
           prop: False

How would I return root, with the full tree structure, but only containing the inner-most sub-records where prop is False?

So i'd expect the query for the above to return:

root:
    id1:
        subId1:
           prop: False

only root and prop are known properties, the iD and subId fields are dynamically generated.

I've added an index for prop however the query i'm using (python) gets me no results:

db.reference('/root').order_by_child("prop").equal_to(False).get()
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
John Bergqvist
  • 852
  • 13
  • 38
  • To get that one node, you could do `db.reference('/root').order_by_child("subId1/prop").equal_to(False).get()`. But I think you want the children on id3 too, including `id3/subId2`, which isn't possible with your current data model. See my answer below if that is indeed what you need. – Frank van Puffelen Aug 01 '19 at 08:07

1 Answers1

0

Firebase returns full nodes from the point where you attach a listener. You can use a query to return a subset of those nodes, but each child node of the location will always be returned in full.

If you find yourself wanting to return partial nodes, consider instead changing your data model to allow the query you want. So in your case, you could create a flat list if the subnodes:

newRoot:
    id1_subId1:
       prop: False
    id1_subId2:
       prop: True
    id3_subId1:
       prop: False
    id3_subId2:
       prop: False

Now you can easily query the new root node and get only the keys with prop: False. Modifying your data model to allow additional use-cases like this is quite common when using NoSQL databases. To learn more I highly recommend reading NoSQL data modeling and watching Firebase for SQL developers.

I also recommend checking out my answers to these related questions:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807