0

firebase database

useEffect(() => {
    fireDb
      .child(`All Ride Requests`)
      .orderByChild(`car_model`)
      .equalTo("Volvo")
      // .orderByValue("car_plaka")
      // .equalTo("S40")
      .on("value", (snapshot) => {
        if (snapshot.val() !== null) {
          setCar({ ...snapshot.val() });
        } else {
          setCar({});
        }
      });
    return () => {
      setCar({});
    };
  }, []);

This way I can't assign 2 queries. Is there a way to do this? Is there a way to query data in the same directory?

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84

1 Answers1

0

Unfortunately, the Realtime database does not support queries on multiple properties, supports only queries on a single child property. So something like this is not possible:

fireDb
  .child(`All Ride Requests`)
  .orderByChild(`car_model`)
  .equalTo("Volvo")
  .orderByChild(`car_plaka`)
  .equalTo("Value")

You can however create a new field which in your database that should look like this:

Firebase-root
   |
   --- All Ride Requests
          |
          --- carModel: "Volvo"
          |
          --- carPlaka: "Value"
          |
          --- carModel_carPlaka: "Volvo_Value"

So as you see, the carModel_carPlaka property combines the values that you want to filter on. In code should look like this:

fireDb
  .child(`All Ride Requests`)
  .orderByChild(`carModel_carPlaka`)
  .equalTo("Volvo_Value")

Unlike the Realtime Database, Cloud Firestore allows compound queries. You should take a look at this. So a query like the one below is allowed in Firestore without creating a combined property.

allRideRequestsRef.where('carModel', '==', 'Volvo').where('carPlaka', '==', 'Value');
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193