Search by array values in Firebase
<p>I use Firebase via REST API. I have following database structure:</p>

<pre><code>{
  "categories" : {
    "Cat1" : {},
    "Cat2" : {},
    "Cat3" : {},
    "Cat4" : {}
  },

  "items" : {
    "item1" : {
      "categories": ["Cat1", "Cat3"]
    },
    "item2" : {
      "categories": ["Cat1", "Cat3"]
    },
    "item3" : {
      "categories": ["Cat1", "Cat2", "Cat3"]
    },
    "item4" : {
      "categories": ["Cat4"]
    }
  }
}
</code></pre>

<p>As you could see we have relations of type "N &lt;-> N" between categories and items (one category could have several items and one item could be in several categories).</p>

<p>Now I want to get all items of Cat1 via Firebase REST API, but I can not do it.</p>

<p>As we know arrays are stored in the Firebase like map with integral indexes:  </p>

<pre><code>"categories": {
  "0": "Cat1",
  "1": "Cat2",
  "2": "Cat3",
}
</code></pre>

<p>So, I added <code>".indexOn": ["categories/*"]</code> to Realtime Database Rules and tried to call this:<br>
<code>curl 'https://...firebaseio.com/...json?orderBy="categories/*"&amp;equalTo="Cat1"'</code></p>

<p>But I got only this: <code>{ }</code></p>

<p>So, I think that regular expressions do not work in Firebase queries, because this worked:<br>
<code>".indexOn": ["categories/0"]</code> in Realtime Database Rules and<br>
<code>curl 'https://...firebaseio.com/...json?orderBy="categories/0"&amp;equalTo="Cat1"'</code></p>

<p>Of course, I could change the database model to something like this:</p>

<pre><code>{
  "categories" : {
    "Cat1" : {},
    "Cat2" : {},
    "Cat3" : {},
    "Cat4" : {}
  },

  "items" : {
    "item1" : {},
    "item2" : {},
    "item3" : {},
    "item4" : {}
  },

  "category-items": {
    "Cat1": ["item1", "item2", "item3"],
    "Cat2": ["item3"],
    "Cat3": ["item1", "item2", "item3"]
    "Cat4": ["item4"]
  }
}
</code></pre>

<p>And get the <code>category-items</code> and iterate through the <code>Cat1</code> array, but then I must to call REST API read method too many times (one REST API call for every <code>item</code> in the category). So, it is too expensive.</p>

<p>So, could anybody help me with getting all items in a category in origin database model?</p>

<p><strong>UPDATE</strong><br>
The final model is:</p>

<pre><code>{
  "categories" : {
    "Cat1" : {},
    "Cat2" : {},
    "Cat3" : {},
    "Cat4" : {}
  },

  "items" : {
    "item1" : {
      "Cat1": true,
      "Cat3": true,
    },
    "item2" : {
      "Cat1": true,
      "Cat3": true,
    },
    "item3" : {
      "Cat1": true,
      "Cat2": true,
      "Cat3": true,
    },
    "item4" : {
      "Cat4": true
    }
  }
}
</code></pre>

<p>Also I added </p>

<pre><code>{
  rules": {
    ...
    "items": {
      ".indexOn": [ "Cat1", "Cat2", "Cat3", "Cat4" ]
    }
  }
}
</code></pre>

<p>to Realtime Database Rules, and REST API call is<br>
<code>curl 'https://...firebaseio.com/items.json?orderBy="Cat1"&amp;equalTo=tr‌​ue'</code></p>

<p>Thanks to Vladimir Gabrielyan</p>