2

I have a firebase model where each object looks like this:

done: boolean
|
tags: array
|
text: string

Each object's tag array can contain any number of strings.

How do I obtain all objects with a matching tag? For example, find all objects where the tag contains "email".

tverghis
  • 957
  • 8
  • 31
  • Your question is similar to http://stackoverflow.com/questions/22506531/how-to-perform-sql-like-operation-on-firebase, so they might be merged once one gets an answer. – Frank van Puffelen Mar 19 '14 at 17:29

1 Answers1

3

Many of the more common search scenarios, such as searching by attribute (as your tag array would contain) will be baked into Firebase as the API continues to expand.

In the mean time, it's certainly possible to grow your own. One approach, based on your question, would be to simply "index" the list of tags with a list of records that match:

/tags/$tag/record_ids...

Then to search for records containing a given tag, you just do a quick query against the tags list:

new Firebase('URL/tags/'+tagName).once('value', function(snap) {
    var listOfRecordIds = snap.val();
});

This is a pretty common NoSQL mantra--put more effort into the initial write to make reads easy later. It's also a common denormalization approach (and one most SQL database use internally, on a much more sophisticated level).

Also see the post Frank mentioned as that will help you expand into more advanced search topics.

Kato
  • 40,352
  • 6
  • 119
  • 149
  • 1
    Has something like this been integrated into the Firebase API? I can't seem to find anything in the docs and this S/O thread was one of the top hits for doing a search like this. – jonobr1 Oct 27 '16 at 00:58