1

In Firebase database I have a list of Bookings.

Each Booking as the following structure

{
   userUid: string,
   status: string,
   moreStuff: {
     ....
   }
}

I need to select all the bookings associated to a certain user (i.e. whose userUid is equal to the uid of the user, which is known by by app) which have a certain status (e.g. status = confirmed).

I can select the Bookings belonging to a specific user using the following query

db.list('bookings', {
        query: {
            orderByChild: 'userUid',
            equalTo: user.uid
        }
    })

but I have no idea if I can add the additional select condition e.g. status = confirmed

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Picci
  • 16,775
  • 13
  • 70
  • 113
  • Firebase Database queries can only order/filter on a single property. You may be able to combine the values that you want to filter on in a single property. See http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase. Alternatively, you can model the data to allow the query. For example, this seems like a categorization problem at first glance, which I covered here: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Feb 21 '17 at 14:32

1 Answers1

1

In a firebase query you can't filter on more than a single field. I have encountered this restriction many times.

I think your challenge would become easier if you model your data differently. I believe that you have a one to many relationship between Users and Bookings. That is, a user may have many bookings but a booking may only have one user. If this is the case, I would create a top level node "bookingLists" which contains separate booking list for each user. The structure is illustrated below. The path "bookingList/< userId >" contains the booking list for a given user. You can access the list if you know the userId. It will contain all bookings for that user. You can then query on a single field in each booking to filter by.

bookingLists
  <userId>
     bookings
        <bookingKey>
            status
            ...other fields

If you need to further filter the booking, you could filter on the client side using observable operations map and filter. This will scale, as long as, each user doesn't have too large a booking list.

Rob Gorman
  • 3,502
  • 5
  • 28
  • 45
  • Thanks. Clear. An additional question though. Isn't the limitation of one field for filter to much of a limitation to use Firebase in medium to complex apps? In my case the solution you suggest works but if I change criteria (e.g. I want the 'pending' bookings for a certain month) I would have to filter on the client side (as you suggest). – Picci Feb 25 '17 at 07:05
  • 2
    Yes its a big limitation. But there are workarounds like combining fields or client side filtering. But its still very inconvenient. But according to the Firebase team its a necessary condition for real scaling. In my case I'm using Firebase not for its scaling, but primarily for its realtime nature and its serverless model. I've found that I can live with the limitations. – Rob Gorman Feb 25 '17 at 19:21