0

I try to retrieve all the grand parent objects when a grandchild key is matched. In the Firebase realtime database below, I have for example the sensor key "-LNVBiM1SGoAFvlMfi1V", and I need to retrieve with it all the orders objects where this key is found (04TBR7 in this example).

I know the database structure might not be perfect but there is no possibility to modify it for now.

I tried the following code but it is not right, I guess the equalTo is not working since it is an Object:

db.ref('/orders').orderByChild('sensors').equalTo(sensor_id).once('value', snapshot => {
// my snapshot is always empty ...
});

enter image description here enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Yoann Hercouet
  • 17,894
  • 5
  • 58
  • 85

1 Answers1

0

There is no way to use orderByChild("sensors") in the way you're trying, as it checks the value of the sensors property (not the key).

It's important to keep in mind that Firebase database queries work on a flat list of nodes. So you can search for a specific order property across all orders, and you can search sensors under a specific order. But you can not search across all sensors for all orders.

Since you say you can't modify the data structure, that unfortunately means you'll have to load the entire JSON and find the matching nodes client-side.

Also see:

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