0

In my app I will have items. Each item belongs to at least one category, but multiple categories are possible. I think the following is the best way to structure the data:

items
    item1
         title: apple
         categories:
             food: true
             fruit: true
    item2
         title: spinach
         categories:
             food: true
             veggie: true
    item3
         title: triceratops
         categories:
             dino: true

How do I query Firebase to return all items matching a category? For example: 'food' should return item1 and item2 and 'veggie' item2. I'm using Angular Fire and tried the following which gives me an error:

$firebaseArray ref.child('items').orderByChild("categories/food")
George Kagan
  • 5,913
  • 8
  • 46
  • 50
DivZero
  • 2,438
  • 2
  • 24
  • 23
  • What error? Also: what version of the Firebase client are you using? (this definitely wasn't possible before 2.3 of the JavaScript client) (I'm not even sure it's possible now, but will have a look once you post more details) – Frank van Puffelen Oct 03 '15 at 13:45
  • @FrankvanPuffelen: "Query.orderByChild failed: First argument was an invalid key: "categories/food". Firebase keys must be non-empty strings and can't contain ".", "#", "$", "/", "[", or "]")." I'm using the latest versions of firebase and angular fire: 2.2.9 and 1.1.2. – DivZero Oct 03 '15 at 13:57
  • This is a query on a nested property, which wasn't possible on Firebase JavaScript versions prior to 2.3. In this case it is also not possible on version 2.3, unless you define an index on each category, i.e. `.indexOn: ["categories/food", "categories/fruit", "categories/veggie", "categories/dino"]` – Frank van Puffelen Oct 03 '15 at 14:01
  • I just tested this and from my experiments it's still not possible. See this post for the latest announcements: https://www.firebase.com/blog/2015-09-24-atomic-writes-and-more.html and this answer for what still seems to be the current situation for your use-case: http://stackoverflow.com/questions/27207059/firebase-query-double-nested/27208492#27208492 – Frank van Puffelen Oct 03 '15 at 14:11
  • I see. I just upgraded to 2.3.1 and 1.1.3 and the error went away. I also added the indices in my security rules. But with the query as in my post it returns all items. – DivZero Oct 03 '15 at 14:15
  • @FrankvanPuffelen I'm open to changing my data structure if that would help. What I need is the following: a) being able to categorise items, b) being able to retrieve all items (with data like title etc) belonging to a category, c) an item can belong to multiple categories. Any suggestions how to achieve this? – DivZero Oct 03 '15 at 14:17
  • 1
    What you'll need is a so-called "custom index". A top-level node `categories`, under which you'd have `food: { item1: true, item2: true}, fruit: { item1: true }, veggie: { item2: true }, dino: { item3: true }`. With that you can do a direct lookup for all foods by `ref.child('categories/food').on('value'` and then load those items. The answer I linked to describes those too. – Frank van Puffelen Oct 03 '15 at 15:00

0 Answers0