Query Value Between Two Other Values in Firebase

Query Firebase Database with two between conditions

A Firebase Database query can only order/filter on one property. That's a hard limit that is not likely to change in the near future.

In some cases it is possible to combine multiple values into a single property, such my answer you linked or in QueryBase. But in those cases as you found, you can only perform a relative/range query on a single of those values: i.e. the last one.

The only case where someone (to my knowing) has ever implemented multi-value range querying is in GeoFire, where we combine the longitude and latitude of a location into a so-called GeoHash. I highly recommend reading more about geohashes, because they do something quite unique: combine two numerical values, into a single string value that allows ordering and filtering on its numeric components.

From looking at the two values you want to combine (length and width), it might be possible to create a similar composite string from them. But requires that you find a way to combine the two values for your use-case. The simplest possible way I can think if would be to store the area, so length * width:

{
items: {
"OshwYF72Jhd9bUw56W7d": {
item_name:"plank_5",
length: 120, // you store length and width as strings, please fix that
width: 50,
area: 6000
}
}
}

That way you can filter for items with an area between 3000 (100x30) and 10500 (70x150):

var query = ref.orderBy("area").startAt(3000).endAt(10500);

This query will match too many items, so you will have to perform some extra filtering client-side to reject the mismatches (Geofire does the same btw):

query.on("child_added", function(snapshot) {
var item = snapshot.val();
if (item.length >= 100 && item.length <= 150
&& item.width >= 30 && item.width <= 70) {
// TODO: do something with the item
}
});

Firebase: How to retrieve two values for the same key query?

You cannot use the AND clause in firebase database, you are only able to query on one language, so you can only do:

orderByChild("language").equalTo("en")

How to compare two values from two different collections from firebase?

As far as I can tell from the massive amount of code you shared, this is the code that adds the vehicle details to the database:

_firestore.collection('Uploading Vehicle Details').add({
'City': uCity,
'Vehicle': uDropdownvalue,
'Description' : description,
'Phone.No#' : phoneNumber,
});

If you want to search that information, you can use a query such as this one:

_firestore.collection('Uploading Vehicle Details')
.where('City', isEqualTo: 'San Francisco')
.where('Vehicle', isEqualTo: 'Toyota RAV4')
.get()
.then(...);

If you want to show this in the UI, you'll want to use snapshots instead of get and wrap it in a StreamBuilder as shown in the documentation on listening to realtime changes.

How to search for more than one value in Firebase

Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. For an example of this and other approaches, see my answer here: Query based on multiple where clauses in Firebase

In your case however, since you want to search for one value across multiple fields, you will need to perform a separate query for each field.



Related Topics



Leave a reply



Submit