Filter array values with Firebase

Multi tool use


Filter array values with Firebase
Is there any way to on Firebae to filter data in an array?
I have this model on my Firebase:
{"orders" : [ null, {
"date" : "2018-07-09 10:07:18",
"item" : [ {
"available" : true,
"name" : "apple",
"price" : 2,
"quantity" : 1
} ]
},
I can filter the date object with the following code:
db.ref('orders').orderByChild('date').startAt("2018-07-09 10:07:18").endAt("2018-07-09 10:07:18")
But how could I do that in the item array? E.g. how could I filter the items with the "name" of apple?
1 Answer
1
That type of query is not possible in your current data structure as Firebase queries only work one level deep in the JSON and your order ID and item index are two level.
If you want to find items by a property, you should store a global list of items, possibly with a duplicated order data. Then you can filter all items on order data or on item name.
Filtering on both name and date is not an API feature, since Firebase queries can only order/filter on one property. You may be able to combine the values into a single synthetic property though, e.g. "item_name_order_date": "apple_2018-07-09"
and then filter on that. For more on this see http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase.
"item_name_order_date": "apple_2018-07-09"
As said: with your current structure you can't query the database for orders with apples. It simply isn't possible. If my answer here doesn't explain well enough, also have a look at stackoverflow.com/questions/27207059/… which deals with the same problem.
– Frank van Puffelen
yesterday
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thank you Frank! But I just wanted to filter the items with the name: "apple". Could you please provide me some source to your approach?
– Róbert Kovács
yesterday