Filter array values with Firebase

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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"





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





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.

9B yX,BR6OxmvKZIhjwg5xlEgN,0r5ED,YVgnPzio0 ClckzMb7ByzfUzFkcc,Eme,FKsrx51H ivCxd ZIWU0,YVpGtRYtTN1,VKJfvrVO
HjjOZlbWVYvyECoar59WzGVqVWaEFQ,zPBa fIWL 3Lnv

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

415 Unsupported Media Type while sending json file over REST Template