-1

How can I make query to create reports to all users with this structure. I read the documentations, but it seems I can't really make those kinds of queries, for eg: I want to query all users that have insurances of animals that are active.

users
  uuid
   ...
   insurances -> Animals
                 1 -> state:active 
                           date:...
                 2 -> state:requested
                           date:...

  ...               

enter image description here

KENdi
  • 7,576
  • 2
  • 16
  • 31
Kevin Dias
  • 1,043
  • 10
  • 28

1 Answers1

1

Firebase queries operate on a flat list of child nodes under the location you query. They can only order/filter on values that are at a known path under each direct child.

Since your values are under insurances/Animals/???, it can't query those values.

You'll typically need to solve this by one of these:

  1. Creating a flat list of all animals for the query. Each animal in this list must have at least the value you want to filter on, but can be a complete duplicate of the animal information under each user.
  2. Giving each user a property that indicates whether they have any insured animal, or the number of insured animals they have, and update that when you update the insurances. That way you can query with firebase.database().ref("users").orderByChild("insuredAnimalCount").startAt(1).

Also see my answer here: Firebase Query Double Nested

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