0

I have a Firebase list of of images with a set of tags in my Angular2 app. I'm looking for a way to return all images with "Tag1". Is this possible using the sort_by function?

let arrayOfElements = 
    [
        {
           "name": "a",
           "subElements": 
           [
             {0: "Tag1"},
             {1: "Tag2"}
           ]
        },
        {
           "name": "a",
           "subElements": 
           [
             {0: "Tag1"},
             {1: "Tag3"}
           ]
        }
    ];
adjuremods
  • 2,938
  • 2
  • 12
  • 17
user3642173
  • 1,225
  • 5
  • 19
  • 42
  • Please have a look at this this might help you http://stackoverflow.com/questions/40844795/how-to-get-a-specific-given-in-firebase/40845176#40845176 – Tabish Hussain Dec 01 '16 at 11:27
  • just replace .orderByChild("name") .equalTo(edittext.getText().toString()); to orderByChild("0").equalTo("Tag1") – Tabish Hussain Dec 01 '16 at 11:28
  • Thanks Tabish! However, the "Tag1" could also have id: 2 for instance. Is it possbile to do this search without knowing the key of the dictionary I'm searching? – user3642173 Dec 01 '16 at 11:37
  • for that you have to separate that Tag1, Tag1, Tag3 in a separate table and only place there ids as keys in the subElement – Tabish Hussain Dec 01 '16 at 12:01
  • I see. Using the javascript filter method seems to work as well but I wonder if this method should be avoided arrayOfElements.map(res => res.filter(r => r. subElements.indexOf("Tag1") > -1)) .subscribe(result => console.log(result)) – user3642173 Dec 01 '16 at 12:15
  • You'll need to create a custom index that maps from each to to the elements matching that tag. See http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Dec 01 '16 at 14:50
  • Also see http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value, which a.o. explains why the array in your structure is problematic (hint: it's a set, not an array). – Frank van Puffelen Dec 01 '16 at 14:55

1 Answers1

3

Now you need to sit and think about this, why? The reason is that you are front a tipical problem in firebase.... Answer this questions:

  • The arrayOfElements can be infinite?
  • The arrayOfElements size can be less of 100 (for example)?

Why you need to answered this questions? Supose that you have an array of 10.000 items (or infinite items) if you use orderByChild("0").equalTo("Tag1") the firebase database needs to reading all items to find the items have "Tag1", then your app/web needs more time and data to get result of this query, then here we need to start talk about normalize firebase database and it's time to create indexes, you can see my yesterday post that I explain it.

If your answer in second question is YES then you can use this command to get values orderByChild("0").equalTo("Tag1") because the data consumed and time wasted is too low front other cases... You can solve this adding indexOn() on firebase database rules. Analize your database and decide the best option.


Update

About your comment... If you doesn't have an id or don't need firebase unique id the best form to do this, for me is this one:

arrayOfElements{
    tag1:"true",
    tag2:"true"
}

And just get the keys as value.

Let me know if I have helped you and good programming!

Community
  • 1
  • 1
  • Thanks for that. In my case it would be <10 elements. However, the "Tag1" could be in any of the keys (0,1... n). As posted above the manual filtering works, but might not be advisable? subElements: arrayOfElements.map(res => res.filter(r => r. subElements.indexOf("Tag1") > -1)) .subscribe(result => console.log(result)) – user3642173 Dec 01 '16 at 12:16
  • My answer is updated, read it and tell me what do you think! ;) – Merlí Escarpenter Pérez Dec 01 '16 at 15:37