2

I was just looking in the docs but couldn't find anything.

So my web app has a structure that's similar to the one in this site.

For the sake of simplicity, let's say my app has only questions which are catalogued by tags. As suggested in the docs, we store our data with a flat, non-normalized structure (E.g.

{
  "questions": {
    ...
  },
  "tags": { 
    "tag1": { 
      "name": "Tag1", 
      "questions": { "0": true, "1": true } 
    }, 
    "tag2": {
      "name": "Tag2",
      "questions": { "2": true, "3": true }
    }
  }
}

), rather than a normalized structure without data replication like:

{ 
  "questions": { 
    "0": { "title": ..., "tag": ... },
    "1": { "title": ..., "tag": ... },  
   }
}

One of the advantages of using the first structure is that I can search for questions that have a certain tag without downloading all the data of all of the questions first: querying for /tags/tag1/questions, will return all the object with all of the question's keys. Now, I can query for the questions, but how do I do that?

I don't want to make ten requests for every question, it seems a waste of time and performance, but I couldn't find a way to make Firebase filter by multiple keys. It seems I can only give Firebase one input at a time. I think (and I hope) I am missing something here. What is it?

If I really can't do this, how do I search by tags here?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
doplumi
  • 2,938
  • 4
  • 29
  • 45
  • OR queries are currently not supported in Firebase. This has been asked before, so I'll probably mark this as a duplicate of one of those. In general if you need to do multiple queries, consider adding an index to prevent. So in this case: **also** store the questions on their own. Data duplication is pretty common in NoSQL databases. – Frank van Puffelen Aug 22 '15 at 15:04
  • 1
    I just realized that you're not actually doing multiple **queries**, just accessing multiple child nodes. Direct access to a node is a very efficient operation, so I wouldn't prematurely optimize it. – Frank van Puffelen Aug 22 '15 at 15:10
  • @FrankvanPuffelen "OR queries aren't supported" Are AND queries supported? Because then I could just retrieve questions that have `tag1 in q.tags AND tag2 in q.tags AND tag3 IN q.tags ...` and so on. Maybe Firebase, isn't gonna do it for this app thought. It's just a shame because it's so simple! – doplumi Sep 01 '15 at 19:42
  • No, AND queries are also not directly supported. But sometimes you can emulate them by combining properties. See http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Sep 01 '15 at 20:31

0 Answers0